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滑块运行时错误


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01