Search code examples
c#wpfwpftoolkitxceedintegerupdown

How do I Reset value of IntegerUpDown to minimum if it exceeds maximum


I have IntegerUpDown like this

<xctk:IntegerUpDown Value ="{Binding SomeValue}", Maximum="2" Minimum="0"/>. 

I was if the value is - SomeValue == 2 (Maximum). I want to set SomeValue = 0 (Minimum) when I click on arrow up in Control.

How can I do that?


Solution

  • You can use the Value changed event to get the current value. At the Value changed event, check for the value, if it is equal to Maximum, set it to Minimum as follows:

    private void IntegerUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var updown = (sender as IntegerUpDown);
            if (updown.Value == updown.Maximum)
                updown.Value = updown.Minimum;
        }
    

    If you are using MVVM, then use interaction behavior or command to do the same.

    or may be, you would bind the Value to a property in your ViewModel, as like you can bind the Max, Min Value to properties in ViewModel. And you can check the value at the property changed event of Value property, and can set the Value on goes above the Maximum value property.