如何使用样式/模板格式化 wpf 中的小数位数? [英] How to format number of decimal places in wpf using style/template?

查看:39
本文介绍了如何使用样式/模板格式化 wpf 中的小数位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 WPF 程序,我试图找出一种方法,通过一些可重复的方法(如样式或模板)来格式化文本框中的数据.我有很多文本框(准确地说是 95 个),每个文本框都绑定到自己的数字数据,每个数据都可以定义自己的分辨率.例如,如果数据是 99.123,分辨率为 2,那么它应该显示 99.12.同样,数据值 99 和分辨率 3 应显示为 99.000(而不是 99).有没有办法做到这一点?

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?

我应该澄清一下,我正在处理的当前屏幕上有 95 个文本框,但我希望程序中各个屏幕中的每个文本框都显示正确的小数位数.现在我考虑了一下,其中一些是 TextBoxes(就像我现在正在处理的屏幕一样),一些是 DataGrids 或 ListViews,但是如果我能弄清楚如何让它为 TextBoxes 工作,我相信我可以弄清楚它也适用于其他控件.

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:

我有一个包含以下属性的视图模型 (vb.net):

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

在我的 XAML 中:

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(我的解决方案):原来,在离开电脑一段时间后,我回来发现一个简单的答案正盯着我的脸.格式化视图模型中的数据!

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

推荐答案

您应该在 Binding 上使用 StringFormat.您可以使用标准字符串格式,或自定义字符串格式:

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}}" />

请注意,StringFormat 仅在目标属性为字符串类型时有效.如果您尝试设置诸如 Content 属性 (typeof(object)) 之类的内容,您将需要使用自定义 StringFormatConverter (like here),并将您的格式字符串作为 ConverterParameter.

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.

编辑更新的问题

因此,如果您的 ViewModel 定义了精度,我建议将其作为 MultiBinding 执行,并创建您自己的 IMultiValueConverter.这在实践中很烦人,从一个简单的绑定到一个需要扩展到 MultiBinding 的绑定,但是如果在编译时不知道精度,这几乎就是你的全部可以做.您的 IMultiValueConverter 需要获取值和精度,并输出格式化的字符串.您可以使用 String.Format 做到这一点.

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.

然而,对于像 ContentControl 这样的东西,你可以用 Style 更容易地做到这一点:

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>

任何公开 ContentStringFormat 的控件都可以这样使用.不幸的是,TextBox 没有类似的东西.

Any control that exposes a ContentStringFormat can be used like this. Unfortunately, TextBox doesn't have anything like that.

这篇关于如何使用样式/模板格式化 wpf 中的小数位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆