Search code examples
silverlightsilverlight-toolkitnumericupdown

Silverlight NumericUpDown ValueChanging triggered twice


I'm using the NumericUpDown control from Silverlight Control Toolkit, that sometimes triger twice the ValueChanging event on one update (clicking just once the upper. or down button). Does anybody know any workarround for this behaviour?


More info:
I have the following xaml:

    <StackPanel>
        <toolkit:NumericUpDown Name="numericUpDown1"
                               Width="35"
                               Minimum="0"
                               Maximum="3"
                               ValueChanging="valueChanging" />
        <toolkit:NumericUpDown Name="numericUpDown2"
                               Width="35"
                               Minimum="0"
                               Maximum="3"
                               ValueChanging="valueChanging" />
        <toolkit:NumericUpDown Name="numericUpDown3"
                               Width="35"
                               Minimum="0"
                               Maximum="3"
                               ValueChanging="valueChanging" />
    </StackPanel>

And the code for the event:

private void valueChanging(object sender, RoutedPropertyChangingEventArgs<double> e)
    {
        if (numericUpDown1.Value + numericUpDown2.Value + numericUpDown3.Value > 3)
        {
            e.Cancel = true;
            MessageBox.Show("No more than 3 in total");
        }
    }

The error comes not all the time, but some times the event is fired twice and the alert comes even when I just have a sum of 3... for example (that not all the time happens): increase to 2 the first one, then to 1 the second, and 1 to the third. a message is showed and it is correct. decrease the firt to 1, and if you increase the third to 1, it will fire the message, because the event is trigerred twice... (again, not all the time this happen)


Solution

  • NumericUpDown uses a RepeatButton which can fire the click event several times. If your NumericUpDown controls don't need to go very high (like in the example you posted where it doesn't accept more a sum larger than 3) you could change the template of the control to use a Button instead.

    Note: I tried changing just the SpinnerStyle template but that didn't work. I had to change the NumericUpDown style and there point to my modified version of the SpinnerStyle