为什么我的货币格式不能正常工作?
Why is my currency format not working properly?
在以下控件中,我附加了一个具有一些默认值的视图模型作为数据上下文:
这是我用作视图模型的 class:
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"/>
尝试像这样在您的绑定中指定美国文化
< TextBlock Text="{Binding Value, StringFormat=C, ConverterCulture=en-US}"/>
在以下控件中,我附加了一个具有一些默认值的视图模型作为数据上下文:
这是我用作视图模型的 class:
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"/>
尝试像这样在您的绑定中指定美国文化
< TextBlock Text="{Binding Value, StringFormat=C, ConverterCulture=en-US}"/>