User cannot type '.' in a text box that was bound to a float value, and UpdateSourceTrigger to PropertyChanged in WPF

I have a funny problem with the Float and UpdateSourceTrigger data type in WPF . I have a property with a float data type and bind it to a TextBox and set the UpdateSourceTrigger bind to PropertyChanged , but WPF doesn't let me type '.' in a TextBox , unless I change UpdateSourceTrigger to LostFocus . I think this is because we cannot enter '.' at the end of the float value. I don’t know how to fix it, because I need to enter '.' and set UpdateSourceTrigger to PropertyChanged .

Property:

  public float? Amount { get;set; } 

And in XAML:

  <TextBox Text="{Binding Amount , UpdateSourceTrigger=PropertyChanged}"/> 
+4
source share
4 answers

Maybe this will help if you add a StringFormat statement to your binding:

 <TextBox Text="{Binding Amount, StringFormat='{}{##.##}', UpdateSourceTrigger=PropertyChanged}"/> 

Update: I saw that my first answer causes some binding errors.

Another option works with the converter (it works, but a little dirty ;-)):

 ... <Window.Resources> <local:FloatConverter x:Key="FloatConverter" /> </Window.Resources> ... <TextBox Text="{Binding Amount, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource FloatConverter}}"></TextBox> 

Converter

 public class FloatConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { // return an invalid value in case of the value ends with a point return value.ToString().EndsWith(".") ? "." : value; } 

}

+2
source

This is because binding to floating point data will automatically cause WPF to add a float validator . You can work around this problem with another DataAnnotation for your float property or write your own Validator .

http://wpf-4-0.blogspot.de/2012/12/data-annotations-in-wpf-c.html

edit: I see that you have a nullable float , so you can also set TargetNullValue to ".".

+1
source

Try adding a StringFormat binding definition to the binding. For instance:

 <TextBox Name="txtPower" Height="23" TextWrapping="Wrap" Text="{Binding Path=Power, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,StringFormat=N2}"></TextBox> 
0
source

If you have installed .NET 4.5 or later, you can apply behavior up to 4.5

 System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; 

In .NET 4.5, you can no longer enter a separator character (comma or period) with UpdateSourceTrigger = PropertyChanged by default.

You may also have a delay:

 <TextBox Width="100" Margin="10" Text="{Binding DoubleField, UpdateSourceTrigger=PropertyChanged,Delay=500, ValidatesOnDataErrors=True}"> 

And another way is to use IValueConverter:

 public class DoubleToPersistantStringConverter : IValueConverter { private string lastConvertBackString; public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!(value is double)) return null; var stringValue = lastConvertBackString ?? value.ToString(); lastConvertBackString = null; return stringValue; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!(value is string)) return null; double result; if (double.TryParse((string)value, out result)) { lastConvertBackString = (string)value; return result; } return null; } } 

check this link: Double field snapping with check

But I think the best way is to set UpdateSourceTrigger to LostFocus .

0
source

Source: https://habr.com/ru/post/1486550/


All Articles