代码之家  ›  专栏  ›  技术社区  ›  Donnelle

组合框项目上的货币格式

  •  2
  • Donnelle  · 技术社区  · 17 年前

    我有一个组合框绑定到一个可见的小数集合。将货币转换器应用于项目的正确方法是什么?

    a) 我有一个必须使用的现有货币转换器

    我需要为项目设置模板吗?

    3 回复  |  直到 17 年前
        1
  •  10
  •   Matt Hamilton    17 年前

    您可以使用ComboBox上的ItemStringFormat属性告诉它如何格式化每个项目:

    <ComboBox ItemStringFormat="c">
    

    但是,请注意,当使用“c”作为货币格式化程序时,它将使用本地计算机定义的货币。如果您的值是以美元定义的,但您的客户机以英镑或日元作为货币符号运行,那么他们将看不到您希望他们看到的东西。

        2
  •  2
  •   Matt Hamilton    17 年前

    <Window.Resources>
        <my:CurrencyConverter x:Key="currencyConverter" />
    
        <DataTemplate x:Key="thingTemplate" DataType="{x:Type my:Thing}">
            <TextBlock
                Text="{Binding Amount,Converter={StaticResource currencyConverter}}" />
        </DataTemplate>
    </Window.Resources>
    
    <ComboBox
        ItemSource="... some list of Thing instances ..."
        ItemTemplate="{StaticResource thingTemplate}" />
    

        3
  •  0
  •   Jobi Joy    17 年前

    使用 字符串格式 在绑定表达式中,如

    <TextBox Text="{Binding Path=Value, StringFormat=Amount: {0:C}}"/>
    

    blog for more details.

    A. ValueConverter