Issue with 2 way bounded Float with leading 0s after the decimal

Jim 21 Reputation points

<local:MoveEntry x:Name="tbQtyToMove" Grid.Row="2" Grid.Column="3" Text="{Binding Qty_To_Move, Mode=TwoWay }" Keyboard="Numeric" Focused="tbQtyToMove_GotFocus" Unfocused="tbQtyToMove_LostFocus" />

I have an Entry that is 2 way bounded to a Float. When the user tries to enter a value like 1.005, the second they enter the first zero the two way binding kicks in and makes the field 1, because 1.0 is updated back as 1. The user never gets the chance to enter the rest, 05. Is there a way to have the binding only happen when you leave the field and not on every key stroke?

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
{count} votes

Accepted answer
  1. John Hardman 261 Reputation points


    Start by changing your setter:

    if (Math.Abs(_Qty_To_Move - value) >= 0.001) // Some threshold value suitable for your scenario
    _Qty_To_Move = value;

    If that doesn't resolve it on its own (you should do that anyway), you might want to try specifying a string format or using a value converter. I think changing the setter should be enough.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful