基于XAML中对象属性的数字形式的货币符号 [英] Currency symbol in numeric value based on object property in XAML

查看:178
本文介绍了基于XAML中对象属性的数字形式的货币符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在WPF中显示数字(货币)值,以使货币符号不会来自用户的区域设置,语言环境或其他任何东西,而不会来自绑定对象的属性.

How to show a numeric (currency) value in WPF so that the currency symbol would not come from the user's regional settings, locale or anything but from the property of the binded object.

例如在DataGrid中,我会有Item个对象:

For example in DataGrid I would have Item objects:

 <DataGrid Grid.Row="1" ItemsSource="{Binding Items}">
      <DataGrid.Columns>
           <DataGridTextColumn Binding="{Binding SomeNumber, StringFormat=c}" />
      </DataGrid.Columns>
 </DataGrid>

public Item()
{
    public double SomeNumber { get; set; }
    public string CurrencySymbol { get; set; }
}

上述标准"c"格式将基于某些用户设置并忽略业务对象来显示货币符号.

The above standard "c"formatting will show currency symbol based on some user setting and ignoring the business object.

但是该值应显示为

SomeNumber = 3456234.67(正数)

SomeNumber = 3456234.67 (positive numbers)

CurrencySymbol ="€" > €3 456 234.67
CurrencySymbol ="$" > $3 456 234.67
CurrencySymbol ="₽" > ₽3 456 234.67

SomeNumber = -3456234.67(负数)

SomeNumber = -3456234.67 (negative numbers)

CurrencySymbol ="€" > (€3 456 234.67)
CurrencySymbol ="$" > ($3 456 234.67)
CurrencySymbol ="₽" > (₽3 456 234.67)

SomeNumber = 0(零值)

SomeNumber= 0 (zero values)

CurrencySymbol ="€" > -
CurrencySymbol ="$" > -
CurrencySymbol ="₽" > -

是否可以通过StringFormat这样将货币符号转换为值?

Is there a way to get the currency symbol to the value this way with StringFormat?

推荐答案

使用StringFormat自动将货币符号附加到数字的正确方法是使用ConverterCulture:

The correct way to automatically attach the currency symbol to a number using StringFormat is by using a ConverterCulture:

<!-- This will be shown as '¥1,423.7' -->
<DataGridTextColumn Header="Amount"
                    Binding="{Binding Path=SomeNumber,
                                      StringFormat=c,
                                      ConverterCulture='ja-JP'}"/>

<!-- This will be shown as '1.423.70 €' -->
<DataGridTextColumn Header="Amount"
                    Binding="{Binding Path=SomeNumber,
                                      StringFormat=c,
                                      ConverterCulture='de-DE'}"/>

关于它的好处是,数字也根据请求的ConverterCulture进行了格式化.

The good thing about it is that the number is also formatted according to requested ConverterCulture.

在这种情况下,您需要自己设置数字的格式(请注意,货币符号的位置也会根据ConverterCulture进行更改),而且 您还想确定将使用哪种货币展示自己 ,因此无法在ConverterCulture上进行中继.

In your case you want to format the number yourself (notice the position of the currency symbol is changing according to the ConverterCulture as well), also, you want to decide what will be the currency displayed yourself, which makes it impossible to relay on the ConverterCulture.

在这种情况下, 正确的方法是自己创建IValueConverter :

public class CurrencyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Item valueAsItem = value as Item;
        if (valueAsItem != null)
        {
            double amount = valueAsItem.SomeNumber;
            if (amount == 0)
            {
                return "-"; //In the question this is the display for 0.
            }
            if (amount < 0)
            {
                amount *= -1; //In the question the numbers always display positive.
            }
            //In the question this is the format for the numbers.
            return valueAsItem.CurrencySymbol + amount.ToString("### ### ###.###");
        }
        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

.xaml中:

  • Window属性内:

<Window.Resources>
    <local:CurrencyConverter 
  x:Key="CurrencyConverter" />
</Window.Resources>

  • 对于DataGrid:

    <DataGrid ItemsSource="{Binding MyItems}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Amount"
                                Binding="{Binding Converter={StaticResource CurrencyConverter}}"/>
            </DataGrid.Columns>
    </DataGrid>
    

  • 现在DataContext如下时:

    public class MyDataContext
    {
        public ICollectionView MyItems { get; set; }
    
        public MyDataContext()
        {
            List<Item> items = new List<Item>
            {
                new Item { CurrencySymbol = "$", SomeNumber = 123.4 },
                new Item { CurrencySymbol = "₹", SomeNumber = 0 },
                new Item { CurrencySymbol = "₹", SomeNumber = -14345623.7 }
            };
    
            MyItems = CollectionViewSource.GetDefaultView(items);
        }
    }
    
    public class Item
    {
        public double SomeNumber { get; set; }
        public string CurrencySymbol { get; set; }
    }
    

    网格看起来像这样:

    根据要求.

    有关double格式上的更多信息MSDN .

    这篇关于基于XAML中对象属性的数字形式的货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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