【问题标题】:Why is my currency format not working properly?为什么我的货币格式不能正常工作?
【发布时间】:2015-07-23 04:17:12
【问题描述】:

在以下控件中,我已将具有一些默认值的视图模型作为数据上下文附加:

这是我用作视图模型的类:

public class ContentViewModel : INotifyPropertyChanged {
    /*Some variables omitted.*/
    private decimal _RoundPrice = 5.00M;

    /*Some properties omitted.*/
    public string Price { get { return string.Format( "{0:C}", this.RoundPrice ); } }
    public decimal RoundPrice {
        get { return this._RoundPrice; }
        set {
            this._RoundPrice = value;
            this.OnPropertyChanged( "Price" );
        }
    }

    private void OnPropertyChanged( string Property ) {
        if ( this.PropertyChanged != null )
            this.PropertyChanged( this, new PropertyChangedEventArgs( Property ) );
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

为了避免必须实现转换器,我选择仅使用属性来转换计数和价格的基础值。我想显示 5.00 美元的价格,但格式由于某种原因省略了货币符号。

我尝试了很多方法让货币显示为美元符号,但都未能正确显示货币。

我的挫败感无法忍受,这是否有一些理由在设计时不显示美元符号?这会在运行时工作,还是我只是被冲洗掉了?

编辑 1

已请求将相关值绑定到显示媒体的方式:

数据上下文是这样绑定的:

<UserControl.DataContext>
    <local:TriviaContentViewModel/>
</UserControl.DataContext>

应该显示价格的控件是这样绑定的:

<Components:WPFGLabel Text="{Binding RoundPrice}" Grid.Row="3" Grid.Column="1" TextAlignment="Left"/>

【问题讨论】:

  • 您是否还有其他货币要显示,或者您只需要玩 $ 吗?以及您是如何在 xaml 文件中绑定的?这很重要
  • 我唯一关心的货币符号是美元符号 ($)。
  • 好的;另外,关于绑定,请参阅编辑:

标签: c# xaml decimal viewmodel currency


【解决方案1】:

尝试像这样在绑定中指定美国文化

< TextBlock Text="{Binding Value, StringFormat=C, ConverterCulture=en-US}"/>

【讨论】:

  • 我爱你,兄弟……哇;那是……我讨厌自己……说真的……谢谢。
  • 没问题@Will 这就是我问你如何在 xaml 中绑定它的原因。运气
  • 所以世界上所有试图对属性进行字符串格式化的尝试都是徒劳的;甚至有必要使用该属性吗?嗯,我敢打赌;一个快速的实验会告诉你......
  • 哦;还;我绑定到了错误的属性/headdesk
  • 对于值是,对于货币编号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多