NumericUpDown doesn't reflect change in bound value

557 views Asked by At

I have a View with a NumericUpDown control, which doesn't work like I expect. Tried Exceed/NumericUpDown and MahApps/NumericUpDown - same problem. It is defined like this:

    <mahapps:NumericUpDown Width="200" Interval="1"
                           Visibility="{Binding Path=ControlValueQuantityIsVisible, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}}">
        <mahapps:NumericUpDown.Value>
            <Binding Mode="TwoWay" Path="ValueQuantity" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged">
                <!-- With or without ValudationRule - same problem -->
            </Binding>
        </mahapps:NumericUpDown.Value>
    </mahapps:NumericUpDown>
    

It is bound to ViewModel's property ValueQuantity

    private double? _valueQuantity;
    public double? ValueQuantity
    {
        get { return _valueQuantity; }
        set
        {
            SetProperty(ref _valueQuantity, value);
            CommandOK.RaiseCanExecuteChanged();
        }
    }

Also, I have a button (Focusable=False) that changes ViewModel's ValueQuantity property to '99'
If I simply press that button, or type values in NumericUpDown, or click its spin buttons - it works fine. Then I select all text in NumericUpDown's textbox and type '1' over it. Then I hit the button - and NumericUpDown still shows '1', even though a breakpoint in ValueQuantity setter shows correct value = 99. Why doesn't NumericUpDown reflect ValueQuantity change in its textbox?

1

There are 1 answers

0
BionicCode On

You have to make the button focusable.

The point is that the input field is a TextBox, which by default updates the binding on lost focus: Binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus.

So, you have to make the input field lose its focus in order to trigger the binding. You must move the focus to another focusable element.