我正在尝试制作一个货币格式的文本框,但它不起作用。
XAML:
<TextBox x:Name="_valueTxt" Text="{Binding Amount, StringFormat={}{0:C}}"/>
代码隐藏:
...
string _amount;
public string Amount
{
get { return _amount; }
set { _amount = value; }
}
...
public MyWindow()
{
Amount = "1235533";
InitializeComponent();
}
我希望在文本框中看到的值:
1.235.533,00 美元
但它显示:
1235533
最佳答案
如果要绑定(bind)到数字,则只能将 StringFormat
与数字格式字符串一起使用。您的 Amount
属性已经是一个 string
,因此您将按原样获取它。
如果您将 Amount
属性更改为数值,您将得到您期望的结果,即:
double _amount;
public double Amount
{
get { return _amount; }
set { _amount = value; }
}
...
public MyWindow()
{
Amount = 1235533;
InitializeComponent();
}
请注意,您可能还想使 Amount
成为 DependencyProperty
或让它实现 INotifyPropertyChanged
。这将允许对值的更改反射(reflect)在用户界面中。
关于wpf - StringFormat 不适用于 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17049612/