c# - 如何使用 StringFormat 舍入绑定(bind)的 double 值

标签 c# wpf xaml data-binding string-formatting

我无法让我的 DataBinding 截断我的 slider double 值的小数位并添加一个 % 符号。我希望即使 slider 值为 89.1234,我的 View 也只显示“89%”。是否可以在 WPF DataBinding 中使用 stringFormat 来实现该行为?

<Label Content="{Binding ElementName=Slider, Path=Value}"/>

非常感谢您的帮助。

最佳答案

您不能使用绑定(bind)的 StringFormat如果您还使用 Label因为Label有一个名为 ContentStringFormat 的属性覆盖绑定(bind)的 StringFormat

要么使用 Label 的 ContentStringFormat属性

<Label Content="{Binding ElementName=Slider, Path=Value}" 
       ContentStringFormat="{}{0:N0}%" />

或者切换到使用 TextBlock

    <TextBlock Text="{Binding ElementName=Slider, Path=Value, StringFormat={}{0:N0}%}" />

关于c# - 如何使用 StringFormat 舍入绑定(bind)的 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9397121/

相关文章:

xaml - 通用Windows应用程序: programmatically change font sizes application wide

WPFToolkit 图表 - 如何删除标记或更改标记大小?

c# - 为什么 ProgressBar.SetValue 会抛出具有有效值的 ArgumentException?

c# - 如何克服自定义文档属性大小限制

c# - 简单的 WPF 绑定(bind)不起作用

c# - 播放列表框中的项目

wpf - 在 Xaml 中控制 WPF 动画

c# - Visual Studio - 字符串插值快捷方式(无需安装 VS 扩展)

c# - '/' 应用程序中的服务器错误

c# - 将控件添加到 Office (VSTO) 中的现有功能区组