0

有没有办法在工具提示中将 ContentStringFormat 用于多种格式?

ContentStringFormat="{}{0:N0}{0:P}"

<Slider.ToolTip> <ToolTip Content="{Binding Path=PlacementTarget.Value, RelativeSource={RelativeSource Mode=Self}}" ContentStringFormat="{}{0:N0}" /> </Slider.ToolTip>

事情就是这样。当我像这样更改 ContentStringFormat 时:

ContentStringFormat="{}{0:N0} {0:P}"

它显示 505,000.00 % 而不是 50 %

提前致谢

4

2 回答 2

2

这里存在三个问题:

  • 如果显示的是 505 而不是 50,则您输入的数字是 5.05 但应该是 0.505
  • 其次,如果您希望 0.505 显示为 50%,则必须先对其进行舍入
  • 最后,你必须使用P0

所以总结一下:

//code
public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();
    DataContext = this;
  }

  private double MyActualNumber
  {
    get { return 0.505; }
  }

  public double Number
  {
    get { return System.Math.Round( 100.0 * MyActualNumber ) / 100.0; }
  }
}

//xaml
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Label Content="{Binding Path=Number}" ContentStringFormat="{}{0:P0}"/>
</Window>
于 2012-07-25T10:14:42.390 回答
1

当您使用标准数字格式 Pp显示的数字首先乘以 100,然后格式化为带有尾随百分比符号的浮点数。紧跟在 之后的精度说明符P,例如P2控制小数位数。见这里

0.5因此,用格式格式化一个值会P2产生字符串“50.00 %”:

您似乎要做的是将值 50 显示为数字 ( N0),后跟百分比 ( P)。结果是字符串“50 5,000.00 %”(因为默认精度为P2)。

于 2012-07-25T10:11:12.543 回答