Search code examples
wpfxamlnumericupdownupdatesourcetrigger

Why the UpdateSourceTrigger=Propertychanged dose not work correctly in IntegerUpdown?


My question is based on How to set a restriction to WPF IntegerUpDown toolkit? that I've posted a while ago.

I have two WPF IntegerUpdown controls, one represents the maximum number and the other represents the minimum number , the first should be greater than the second and the second should be less than the first.

The given solution by @D M in the first question solve my problem

    <wpfToolkit:IntegerUpDown x:Name="minimumatt" 
                          Value="0" 
                          Minimum="0" />
                                    
<wpfToolkit:IntegerUpDown x:Name="maximumatt" 
                          Value="0" 
                          Minimum="0" 
                          Maximum="{Binding ElementName=minimumatt, 
                                            Path=Value
                                            UpdateSourceTrigger=PropertyChanged}" />

But this can be only one when I click outside the control , the scenario that should be achieved is when the user selects a number in minimum IntegerUpdown control greater than the number that already exist in maximum , the number should be set to 0 or default when the user lost focus by mouse at that moment.

I already tested this solution:

 Maximum="{Binding ElementName=minimumatt, 
                                                Path=Value
                                                UpdateSourceTrigger=LostFocus}"

But the event does not fired.

What can I do in this case?

For example , in this case when the user put 18 in min , the value should be changed in lost focus event, what worked now is that the user has to click outside this control to get the default value in minimum IntegerUpdown

enter image description here

Update:

I found an event (Mouseleave_event) that can be used to update the value in real time ( without clicking on other controls ) but I did not have any idea how can I use this event with Propertychanged , how can I do this?


Solution

  • I found an event (Mouseleave_event) that can be used to update the value in real time ( without clicking on other controls ) but I did not have any idea how can I use this event with Propertychanged , how can I do this?

    UpdateSourceTrigger is a property of the binding that controls when the source property, Value in this case, is updated. It doesn't raise any MouseLeave event for you.

    If you want to handle an event, you should hook up an event handler for it and implement the handler in your code-behind class as usual:

    <wpfToolkit:IntegerUpDown x:Name="maximumatt" 
                              Value="0" 
                              Minimum="0" 
                              Maximum="{Binding Value, ElementName=minimumatt}"
                              MouseLeave="maximumatt_MouseLeave"/>