WPF Slider run time error(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滑块运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WPF滑块运行时错误
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
