以两位小数显示货币,但保留所有可用的小数 [英] Display Currency in two decimals, but keeping all available decimals

查看:93
本文介绍了以两位小数显示货币,但保留所有可用的小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速(我希望)问题。
我有这个DataGrid,其中包含一些货币列。我似乎找不到如何以2位小数显示货币。但是在单元格中保留可能的3或4个十进制值。如果尝试了一些字符串格式设置,但实际上更改了该值。当然,IValueConverter也是如此。那我想念什么呢?

Quick(I hope) question. I've got this DataGrid, containing a few currency column's. What I can't seem to find is how to display the currency in 2 decimals. But maintaining a possible 3 or 4 decimal value in the cell. If tried some string-formatting but that actually changes the value. And so does a IValueConverter of course. So what am I missing? or could anybody point me in the right direction?

编辑:
建议之后,我尝试了类似的操作。

after the suggestion I tried something like this.

 <Style x:Key="DecimalTextBlockStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:C}}"/>
        <Setter Property="TextBlock.Foreground" Value="Red"/>
        <Setter Property="Background" Value="AliceBlue"/>
    </Style>

并在此处申请:

if(e.PropertyType.FullName.Contains("Decimal"))
        {
            var cl = e.Column as DataGridTextColumn;
            if (cl != null)
            {
                //cl.Binding.StringFormat = "C2";
                Style Stl = (Style)FindResource("DecimalTextBlockStyle");
                cl.CellStyle = Stl;

            }}

但是我没有得到的是为什么!是那些列中的值,漂亮的文本和红色的文本,蓝色的背景..但未格式化的字符串,仅显示0,000而不是€0,00

But what I Dont Get is WHY! are the value's in those columns nice and red text, blue background.. but unformated string, just shows 0,000 in stead of €0,00

推荐答案

使用 StringFormat 或单向 Converter 不应更改实际值。两者都仅用于显示目的。

Using StringFormat or a one-way Converter should not change the actual value. Both of those are only used for display purposes.

例如,这将以货币格式显示带有两个小数位的十进制值,但不会修剪 SomeDecimal 到两位小数位

For example, this will show the decimal value in currency format with 2 decimal places, but it will not trim SomeDecimal to two-decimal places

<TextBox Text="{Binding SomeDecimal, StringFormat=C2}" />

也许您希望使用2位数字显示值,但使用全4位数字对其进行编辑。如果是这种情况,我建议使用样式触发器在编辑时显示未格式化的值。

Perhaps you are looking to display a value using 2 digits, but edit it using the full 4 digits. If this is the case, I would suggest using a Style Trigger to show the unformatted value while it's being edited.

<Style x:Key="DecimalTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Text" Value="{Binding SomeDecimal, StringFormat=C2}" />
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="Text" Value="{Binding SomeDecimal}" />
        </Trigger>
    </Style.Triggers>
</Style>

编辑

按照下面的请求将此样式应用于 DataGridCell ,您可以使用 DataGridTemplateColumn 并放置一个TextBox在其中使用此样式的样式,也可以使用 ElementStyle EditingElementStyle 属性设置绑定格式

Per your request below to apply this style to a DataGridCell, you can either use a DataGridTemplateColumn and place a TextBox inside it with this style applied, or you can use the ElementStyle and EditingElementStyle properties to set the binding format

<DataGridTextColumn>
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="{Binding SomeDecimal, StringFormat=C2}" />
        </Style>
    </DataGridTextColumn.ElementStyle>
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding SomeDecimal}" />
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

这篇关于以两位小数显示货币,但保留所有可用的小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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