【发布时间】: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