WPF滑块运行时错误 [英] WPF Slider run time error

查看:27
本文介绍了WPF滑块运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有 2 个滑块.我的第二个滑块绝不允许小于我的第一个滑块,因此如果有人试图将第二个滑块向下滑动超过第一个滑块,第一个滑块将始终等于第二个滑块.

I have 2 sliders in my program. My second slider is never allowed to be less than my first slider, so if someone were to try to slide the second slider down past the first one, the first one would always equal the second one.

我正在用 C# 编写代码,但我不明白为什么这段代码不起作用:

I'm coding this in C#, and I don't understand why this code does not work:

//SLIDER 1
        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (slider2.Value <= slider1.Value)
                slider1.Value = slider2.Value;
        }

XAML - 编译器说的我的第二个滑块在运行时是 null:

XAML - My second slider that the compiler says is null at runtime:

<Slider Height="22" Margin="128,45,130,0" Name="slider2" VerticalAlignment="Top" Maximum="160" Minimum="1" TickFrequency="1" TickPlacement="BottomRight" Value="50" IsSnapToTickEnabled="True" ValueChanged="slider2_ValueChanged" />

编译器说 NullReferenceException 未被用户代码处理对象引用未设置为对象的实例.我需要做些什么才能使其正常工作?

The compiler says NullReferenceException was unhandled by user code, Object reference not set to an instance of an object. What do I need to do to get this working?

谢谢.

推荐答案

来吧,这很简单.编程基础... O_o

Come on, this is an easy one. Basics of programming... O_o

在使用它们之前,只需检查两个控件的 null.

Simply check both controls for null before using them.

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    if (slider1 == null || slider2 == null)
        return;

    if (slider2.Value <= slider1.Value)
        slider1.Value = slider2.Value;
}

这篇关于WPF滑块运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆