简体   繁体   中英

MahApps.Metro NumericUpDown events workaround

I am experiencing an odd issue, and have not found anyone else experiencing similar from my searches. What I have is a NumericUpDown and I simply want to call a method when either the + or - buttons are pressed. So i could see 2 options : ValueDecremented & ValueIncremented events or, better yet ValueChanged event. But it's not behaving as expected,on either option. I have stripped it right down to just display a messagebox with the value as follows :

private void num_ZoneDistance_ValueIncremented(object sender, MahApps.Metro.Controls.NumericUpDownChangedRoutedEventArgs args)
{
        MessageBox.Show(num_ZoneDistance.Value.ToString());
}
private void num_ZoneDistance_ValueDecremented(object sender, MahApps.Metro.Controls.NumericUpDownChangedRoutedEventArgs args)
{
        MessageBox.Show(num_ZoneDistance.Value.ToString());
}

And alternatively :

private void num_ZoneDistance_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
{
        MessageBox.Show(num_ZoneDistance.Value.ToString());
}

But with the first set, it's 1 out. ie. If it's on 4 and I press +, it reports 4 THEN updates to 5, which is not what I'd expect, if it only updates after the associated event, what's the point?

With the second option, ValueChanged, it reports the correct - updated - value, however the event fires when the window loads( as well as at correct time), during component initialization - and that causes crashes because dependent objects are created yet - and i have no idea why the event would trigger onload.

The only workaround I have found is to user the first option, and manual add/substract 1 from the value, but does anyone have a better suggestion? because surely both options should just work anyway, as I understand them.

Thanks

EDIT : Going with num_ZoneDistance_ValueChanged event And then placed inside it a check on num_ZoneDistance.Value!=null Avoids the crash on loading up, doesn't explain why the event is triggered at that point, but maybe it's me misinterpreting expected behavior, either way checking it isn't null circumvents the issue, and ValueChanged reports correct values unlike ValueIncremented & ValueDecremented, that both report the previous value, and it reduces it to 1 method instead of 2.

You should use the Interval property of the NumericUpDownChangedRoutedEventArgs to show or work with it.

private void num_ZoneDistance_ValueIncremented(object sender, MahApps.Metro.Controls.NumericUpDownChangedRoutedEventArgs args)
{
    MessageBox.Show(args.Interval.ToString());
}

private void num_ZoneDistance_ValueDecremented(object sender, MahApps.Metro.Controls.NumericUpDownChangedRoutedEventArgs args)
{
    MessageBox.Show(args.Interval.ToString());
}

If you need it, you can change the Interval property at the events.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM