Uchecked事件与一个处理程序

Uchecked事件与一个处理程序

本文介绍了WPF切换按钮检查/ Uchecked事件与一个处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WPF窗口中使用 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?

我将有许多 ToggleButton s,代码可以变大

I will have many ToggleButtons, and the code can get large.

推荐答案

您可以附加 ToggleButton 的单击事件在其处理程序中,您可以通过类型转换处理程序中的发件人对象来查看 ToggleButton 属性。 p>

You can attach a single click event of your ToggleButton and in its handler you can check the ToggleButton IsChecked property by type casting the sender object in your handler like this -

private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
   if((sender as ToggleButton).IsChecked)
   {
      // Code for Checked state
   }
   else
   {
      // Code for Un-Checked state
   }
}

Xaml:

<ToggleButton Height="37" HorizontalAlignment="Left" Margin="485.738,254.419,0,0"     VerticalAlignment="Top" Width="109" IsEnabled="True" Click="ToggleButton_Click">Timeout</ToggleButton>

这篇关于WPF切换按钮检查/ Uchecked事件与一个处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:15