如何使用样式/模板格式化wpf中的小数位数? [英] How to format number of decimal places in wpf using style/template?
问题描述
编辑:
我应该澄清,当前屏幕上有95个TextBoxes我' m,但是我希望我的程序中的各个屏幕中的每个TextBox显示正确的小数位数。现在我想了一下,其中一些是TextBox(像我现在正在工作的屏幕),有些是DataGrids或ListViews,但是如果我能弄清楚如何使它在TextBox中工作,我相信我可以对于其他控件来说,它也是如此。
在这种情况下,没有多少代码可以分享,但我会尝试更清楚:
我有一个视图模型,其中包含以下属性(vb.net):
Public ReadOnly属性分辨率为整数
获取
返回_signal.DisplayResolution
结束获取
结束属性
公共ReadOnly属性值作为单个
获取
返回Math.Round(_signal.DisplayValue,Resolution)
结束获取
结束属性
在XAML中我有:
< UserControl.Resources>
< vm:SignalViewModel x:Key =SignalSignalPath =SomeSignal/>
< /UserControl.Resources>
< TextBox Grid.Column =3IsEnabled =FalseText ={Binding Path = Value,Source = {StaticResource Signal},Mode = OneWay}/>
EDIT2(我的解决方案):
事实证明在离开电脑一段时间后,我回来找到一个简单的答案,盯着我的脸。格式化视图模型中的数据!
公共ReadOnly属性值作为字符串
获取
返回(字符串.FormatNumber(Math.Round(_signal.DisplayValue,_signal.DisplayResolution),_signal.DisplayResolution))
结束获取
结束属性
您应该使用绑定 c
/ code>。您可以使用标准字符串格式或自定义字符串格式:
< TextBox Text ={Binding Value,StringFormat = N2}/>
< TextBox Text ={Binding Value,StringFormat = {} {0:#,#。00}}/>
请注意, StringFormat
仅在target属性是string类型。如果您正在尝试设置类似于内容
属性( typeof(object)
)的内容,则需要使用自定义 StringFormatConverter
( like here < a>),并将您的格式字符串作为 ConverterParameter
。
strong>
所以,如果你的 ViewModel
定义精度,我建议做一个 MultiBinding
,并创建自己的 IMultiValueConverter
。这在实践中非常烦人,从简单的绑定到需要扩展到 MultiBinding
的绑定,但如果在编译时不知道精度,这几乎是你可以做的。您的 IMultiValueConverter
将需要取值和精度,并输出格式化的字符串。您可以使用 String.Format
执行此操作。
但是,对于像 ContentControl
,您可以轻松地使用样式
:
< Style TargetType ={x:Type ContentControl}>
< Setter Property =ContentStringFormat
Value ={Binding Resolution,StringFormat = N {0}}/>
< / Style>
显示 ContentStringFormat
的任何控件都可以这样使用不幸的是, TextBox
没有这样的东西。
I am writing a WPF program and I am trying to figure out a way to format data in a TextBox through some repeatable method like a style or template. I have a lot of TextBoxes (95 to be exact) and each one is bound to its own numeric data which can each have their own resolution defined. For example if the data is 99.123 with a resolution of 2 then it should display 99.12. Similarly a data value of 99 and resolution 3 should be displayed as 99.000 (not 99). Is there a way to do this?
Edit: I should clarify, there are 95 TextBoxes on the current screen I'm working on, but I want every TextBox throughout the various screens in my program to display the correct number of decimal places. Now that I think about it, some of these are TextBoxes (like the screen I'm working on now) and some are DataGrids or ListViews, but if I can figure out how to get it working for TextBoxes I'm sure I can figure it out for the other controls as well.
There's not much code to share in this case but I'll attempt to make it clearer:
I have a View Model which contains the following properties (vb.net):
Public ReadOnly Property Resolution As Integer
Get
Return _signal.DisplayResolution
End Get
End Property
Public ReadOnly Property Value As Single
Get
Return Math.Round(_signal.DisplayValue, Resolution)
End Get
End Property
and in the XAML I have:
<UserControl.Resources>
<vm:SignalViewModel x:Key="Signal" SignalPath="SomeSignal"/>
</UserControl.Resources>
<TextBox Grid.Column="3" IsEnabled="False" Text="{Binding Path=Value, Source={StaticResource Signal}, Mode=OneWay}" />
EDIT2 (my solution): It turns out that after walking away from the computer for a while, I came back to find a simple answer that was staring me in the face. Format the data in the view model!
Public ReadOnly Property Value As String
Get
Return (Strings.FormatNumber(Math.Round(_signal.DisplayValue, _signal.DisplayResolution), _signal.DisplayResolution))
End Get
End Property
You should use the StringFormat
on the Binding
. You can use either standard string formats, or custom string formats:
<TextBox Text="{Binding Value, StringFormat=N2}" />
<TextBox Text="{Binding Value, StringFormat={}{0:#,#.00}}" />
Note that the StringFormat
only works when the target property is of type string. If you are trying to set something like a Content
property (typeof(object)
), you will need to use a custom StringFormatConverter
(like here), and pass your format string as the ConverterParameter
.
Edit for updated question
So, if your ViewModel
defines the precision, I'd recommend doing this as a MultiBinding
, and creating your own IMultiValueConverter
. This is pretty annoying in practice, to go from a simple binding to one that needs to be expanded out to a MultiBinding
, but if the precision isn't known at compile time, this is pretty much all you can do. Your IMultiValueConverter
would need to take the value, and the precision, and output the formatted string. You'd be able to do this using String.Format
.
However, for things like a ContentControl
, you can much more easily do this with a Style
:
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentStringFormat"
Value="{Binding Resolution, StringFormat=N{0}}" />
</Style>
Any control that exposes a ContentStringFormat
can be used like this. Unfortunately, TextBox
doesn't have anything like that.
这篇关于如何使用样式/模板格式化wpf中的小数位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!