WPF滑块运行时错误

WPF Slider run time error(WPF滑块运行时错误)
本文介绍了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滑块运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)