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?
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");
}
}
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.
If you have .NET 4.5 or newer, you may enforce the pre 4.5 behaviour
System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
See Sebastian Lux's blog:
With .NET 4.5 it is no longer possible to enter a separator character (comma or dot) with UpdateSourceTrigger = PropertyChanged
by default. Microsoft says, this intended.
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>
to fix dot and comma issue in textbox binding to decimal or float
1- UpdateSourceTrigger = LostFocus
2- add string format StringFormat={}{0:#.##} to escape unneeded zeros
<TextBox Name="txtPower" Height="23"
TextWrapping="Wrap" Text="{Binding Path=Power, Mode=TwoWay,
UpdateSourceTrigger=LostFocus ,StringFormat={}{0:#.##}}"></TextBox>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With