带有一个处理程序的 WPF 切换按钮 Checked/Uchecked 事件
我在 WPF 窗口中使用 ToggleButton
:
<ToggleButton Height="37"
HorizontalAlignment="Left"
Margin="485.738,254.419,0,0"
VerticalAlignment="Top"
Width="109"
IsEnabled="True"
Checked="toggleAPDTimeoutErr_Checked"
Unchecked="toggleAPDTimeoutErr_Unchecked">Timeout</ToggleButton>
我正在监视两个事件,但这是在处理程序后面的两个不同代码中完成的。这怎么能只用一个来完成呢?
我将有许多 ToggleButton
,并且代码可能会变得很大。
I am using a ToggleButton
in a WPF window:
<ToggleButton Height="37"
HorizontalAlignment="Left"
Margin="485.738,254.419,0,0"
VerticalAlignment="Top"
Width="109"
IsEnabled="True"
Checked="toggleAPDTimeoutErr_Checked"
Unchecked="toggleAPDTimeoutErr_Unchecked">Timeout</ToggleButton>
I have two events that I am monitoring, but this is done in two different code behind handlers. How can this be done in only one?
I will have many ToggleButton
s, and the code can get large.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以附加
ToggleButton
的单击事件,并且在其处理程序中,您可以通过在您的像这样的处理程序 -Xaml:
You can attach a single click event of your
ToggleButton
and in its handler you can check theToggleButton
IsChecked
property by type casting the sender object in your handler like this -Xaml:
您不应该像一些答案所建议的那样使用
Click
事件,因为当属性IsChecked
被代码或鼠标以外的任何其他事件(键盘、动画.. )。这只是一个错误。相反,您可以对
Checked
和Unchecked
使用相同的处理程序,并根据IsChecked
属性执行操作。You should not use
Click
event as some answers suggest, because it will not work when the propertyIsChecked
is changed by code or any other event than mouse (keyboard, animation..). This is simply a bug.Instead you can use the same handler for both
Checked
andUnchecked
and do action depending onIsChecked
property.试试这个
Try this