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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
LukeSolar
  • 3,795
  • 4
  • 32
  • 39

3 Answers3

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.

xmedeko
  • 7,336
  • 6
  • 55
  • 85
  • The link is broken, which of course is why you shouldn't provide link-only answers. – A.R. Feb 25 '19 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 '19 at 07: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 '19 at 20:05
  • 1
    just a note that this will affect all textboxes. https://social.msdn.microsoft.com/Forums/vstudio/en-US/92ae5178-4a92-4784-b30b-226ecb1eb146/decimal-binding-issue-in-wpf – walterhuang Feb 28 '19 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>
Ravi Y
  • 4,296
  • 2
  • 27
  • 38
yonigozman
  • 707
  • 8
  • 15
  • 3
    I used StringFormat={}{##.##} and this works for me. Not sure whats N2. Thanks! – LukeSolar Jan 30 '13 at 10:56
  • 1
    @LukeSolar here is a answer for you https://stackoverflow.com/questions/4506323/difference-between-tostringn2-and-tostring0-00 – huoxudong125 Jan 27 '15 at 01:16
  • 14
    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 '15 at 04: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 03 '17 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 03 '17 at 15:45
  • @LuckyLikey Sorry, dont remember any more, was long time ago and I believe someone else looked at the issue – pixel Aug 03 '17 at 16:04
  • Oddly enough, with added `StringFormat` the textbox initially is empty. When I remove StringFormat it correctly shows its initial value (initial value is round one, "1", no decimal points); – astrowalker Jan 23 '20 at 07:40
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>
Hisham
  • 1,279
  • 1
  • 17
  • 23