Search code examples
c#wpfxamlintegerupdown

How to set a restriction to WPF IntegerUpDown toolkit?


I have two WPF IntegerUpDown toolkit and I would like to set to one of the toolkit a restriction , the maximum of the second IntegerUpDown toolkit should be equals to the value of the first IntegerUpDown toolkit.

I choose Value_changed event to do this , but I got this exception

System.NullReferenceException: 'Object reference not set to an instance of an object.'

This my code :

   private void minimumatt_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        
        {
            maximumatt.Maximum = minimumatt.Value;

        }

Xaml code:

   <wpfToolkit:IntegerUpDown  ValueChanged="minimumatt_ValueChanged" Value="0" Minimum="0" x:Name="minimumatt" MinWidth="100" />
                                    
   <wpfToolkit:IntegerUpDown Value="0" Minimum="0" x:Name="maximumatt" MinWidth="100"   />

How can I add a restriction to the IntgerUpDown WPF toolkit control?


Solution

  • Is there any reason you cannot bind the Maximum directly to the Value of the other control?

    <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}" />