7

I have a funny problem with Float data type and UpdateSourceTrigger in WPF.I have a property with float data type and bind it to a TextBox and set UpdateSourceTrigger of the Binding to PropertyChanged,but WPF dosen't let me type '.' in the TextBox unless i change UpdateSourceTrigger to LostFocus.I think it's because of we can not type '.' in the end of float value.I don't have any idea how can i fix it because i need to type '.' and set UpdateSourceTrigger to PropertyChanged.

The property is:

  public float? Amount
    {
        get;set;
    }

And in the XAML:

    <TextBox
        Text="{Binding Amount , UpdateSourceTrigger=PropertyChanged}"/>
Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47
mahboub_mo
  • 2,908
  • 6
  • 32
  • 72
  • Duplicate to http://stackoverflow.com/questions/14600842/bind-textbox-to-float-value-unable-to-input-dot-comma/35942615#35942615 – xmedeko Mar 11 '16 at 14:24

4 Answers4

3

Maybe it would help if you add a StringFormat statement in your binding:

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

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

An other option is working with a converter (works, but a bit 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;
  }

}

rhe1980
  • 1,557
  • 1
  • 15
  • 36
  • This problem is a Net5 bug,the is no problem with typing dot in float values in net4.5.And this answer wont let user to type zero after dot. – mahboub_mo Mar 06 '14 at 07:16
  • Yoy may fallback to the previous behaviour, see http://stackoverflow.com/a/35942615/254109 – xmedeko Mar 11 '16 at 14:23
1

This is because binding to a float data Type will automatically cause WPF to add a float validator. You can bypass this problem by use a different 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 you have a nullable float, so you can also try to set the TargetNullValueto ".".

Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47
1

If you have .NET 4.5 or newer, you may enforce the pre 4.5 behaviour

System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;

With .NET 4.5 it is no longer possible to enter a separator character (comma or dot) with UpdateSourceTrigger = PropertyChanged by default.

Also you can have a delay :

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

And another way is 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: Binding to double field with validation

But i think the best way is that you set UpdateSourceTrigger to LostFocus.

da jowkar
  • 181
  • 1
  • 8
  • +1 Delay=500 to sidestep the validation issue and still keep the updatesourcetrigger to propertychanged. – Ben Adams Feb 25 '22 at 00:15
0

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>
Mohd Ahmed
  • 1,422
  • 13
  • 27