简体   繁体   English

MahApps.Metro NumericUpDown事件解决方法

[英]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. 我所拥有的是一个NumericUpDown,我只想在按下+或-按钮时调用一个方法。 So i could see 2 options : ValueDecremented & ValueIncremented events or, better yet ValueChanged event. 所以我可以看到2个选项:ValueDecremented和ValueIncremented事件,或者更好的是ValueChanged事件。 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? 如果它在4上并且我按+,它将报告4 THEN更新到5,这不是我期望的,如果它仅在相关事件之后更新,那有什么意义呢?

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. 使用第二个选项ValueChanged,它报告正确的-更新的-值,但是该事件在组件初始化期间在窗口加载时(以及在正确的时间)触发,并且由于未创建依赖对象而导致崩溃-和i不知道为什么该事件会触发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? 我发现的唯一解决方法是使用第一个选项,并从值中手动添加/减去1,但是有人有更好的建议吗? 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. 编辑:处理num_ZoneDistance_ValueChanged事件,然后在其中检查num_ZoneDistance.Value!= null避免加载时崩溃,没有解释为什么在该点触发了该事件,但是也许是我错误地解释了预期的行为检查它是否不为null可以解决问题,并且ValueChanged报告与ValueIncremented和ValueDecremented不同的正确值,它们均报告先前的值,并将其减小为1方法而不是2。

You should use the Interval property of the NumericUpDownChangedRoutedEventArgs to show or work with it. 您应该使用NumericUpDownChangedRoutedEventArgsInterval属性来显示或使用它。

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. 如果需要,可以在事件中更改Interval属性。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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