绑定文本框浮动值。无法输入点/逗号 [英] Bind textbox to float value. Unable to input dot / comma
问题描述
当我尝试输入一个点或一个文本框逗号,例如 1.02
或 83,33
文本框prevents我输入这样一个值(和输入变为红色)。该文本框绑定到一个float属性。为什么呢?
When I try to input a DOT or a COMMA in a textbox, for example 1.02
or 83,33
the textbox prevents me to input such a value (and the input turns red). The textbox is bound to a float property. Why?
我已绑定的文本框为float属性电源
A类实现 INotifyPropertyChanged的
的。
I have bound a textbox to a float Property Power
of a class implementing INotifyPropertyChanged
.
private float _power;
public float Power
{
get { return _power; }
set
{
_power = value;
OnPropertyChanged("Power");
}
}
在XAML
In Xaml
<TextBox Name="txtPower" Height="23" TextWrapping="Wrap" Text="{Binding Path=Power, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
我没有自定义的验证,在现在没事了。
I have no custom validation at all right now.
也试过小数,但它并不能工作。对于字符串一切工作正常。
Also tried decimal but it does not work either. For string everything works fine.
推荐答案
尝试添加的StringFormat定义绑定。 像这样:
Try adding a StringFormat definition to the binding. Like so:
<TextBox Name="txtPower" Height="23"
TextWrapping="Wrap" Text="{Binding Path=Power, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,StringFormat=N2}"></TextBox>
这篇关于绑定文本框浮动值。无法输入点/逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!