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

33

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The link is broken, which of course is why you shouldn't provide link-only answers.
@A.R. This is the full contained answer. Just one line of code. The link is just for credits.
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.
just a note that this will affect all textboxes. social.msdn.microsoft.com/Forums/vstudio/en-US/…
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 Comments

I used StringFormat={}{##.##} and this works for me. Not sure whats N2. Thanks!
@LukeSolar here is a answer for you stackoverflow.com/questions/4506323/…
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 I experience the same behavior and IHMO this is unuseable. Did you find any solution to this, except changing the UpdateSourceTrigger to LostFocus ?
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.
|
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>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.