43

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.

3 Answers 3

32

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.

4
  • The link is broken, which of course is why you shouldn't provide link-only answers.
    – A.R.
    Feb 25, 2019 at 23:43
  • @A.R. This is the full contained answer. Just one line of code. The link is just for credits.
    – xmedeko
    Feb 26, 2019 at 7:18
  • 1
    It just suggests "try this" meaning it may not work. No explanation, no context. just a broken link. If you think the one liner does it all, at least make an edit an remove the broken link.
    – A.R.
    Feb 27, 2019 at 20:05
  • 1
    just a note that this will affect all textboxes. social.msdn.microsoft.com/Forums/vstudio/en-US/… Feb 28, 2019 at 19:15
30

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>
7
  • 3
    I used StringFormat={}{##.##} and this works for me. Not sure whats N2. Thanks!
    – LukeSolar
    Jan 30, 2013 at 10:56
  • 1
    @LukeSolar here is a answer for you stackoverflow.com/questions/4506323/… Jan 27, 2015 at 1:16
  • 15
    While this resolve the issue of entering something like 1.1, it adds multiple issues such as number is always displayed with decimals (i.e 1 is shows as 1.00), using backspace or delete keys in box will not work for some reason. Placing cursor befor dot, then typing .11 results in 1.11.11
    – pixel
    Dec 17, 2015 at 4:17
  • @pixel I experience the same behavior and IHMO this is unuseable. Did you find any solution to this, except changing the UpdateSourceTrigger to LostFocus ?
    – LuckyLikey
    Aug 3, 2017 at 15:38
  • For anyone struggling with the StringFormat if you need UpdateSourceTrigger=PropertyChanged a possible workaround is to bind to a string property and then validating that string inside the setter. You can also logically connect it to a float, double, or decimal property, so both values will syncronize.
    – LuckyLikey
    Aug 3, 2017 at 15:45
1

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>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.