我现在项目中需要使用到WPF中的CheckBox,我需要或许到选中的事件以及取消选中的事件:
 
         void cbDenoise_Checked(object sender, RoutedEventArgs e)
        {
            bool? isSelect = this.userCtrlTargetMeasurementArea1.cbDenoise.IsChecked;            System.Windows.Forms.MessageBox.Show(isSelect.ToString());
        }但是现在的情况是,在我选中的时候会发生选中的事件,但是取消选中就没有事件发生,我想请问一下,应该怎么做才能使选中CheckBox和取消选中CheckBox都产生事件呢?现在我的情况是System.Windows.Forms.MessageBox.Show(isSelect.ToString());
这个isSelect只有在选中的时候才会弹出来为True,但是取消的情况下却没有事件产生?我想请问下,这是什么原因呢?

解决方案 »

  1.   

    WPF CheckBox Control:
    CheckBox in the user interface (UI) of WPF application is used to represent options that a user can select or clear. You can use a single check box or you can group two or more check boxes so that users can choose any number of options from a list of options.
     
    In XAML CheckBox tag represents the CheckBox Control.
     
    < CheckBox></ CheckBox>
     
    Properties:
    With the following tag I created the CheckBox, in which Height is height of the checkbox, Name is the nameof the checkbox. Background the the color of the box, Borderbrush is the color of the border, Forground is the color of the text.
     
    <CheckBox Height="15" Margin="16,17,122,0" Name="chkA2zdotnet" VerticalAlignment="Top" Background="Cyan" BorderBrush="Blue" Foreground="Chocolate" FlowDirection="LeftToRight" HorizontalContentAlignment="Left" VerticalContentAlignment="Center">A2ZDotnet.com</CheckBox>
     
    It would look like 
    Now if you want the checkbox to be on the right side instead of the left side set the FlowDirection property to "RightToLeft". Then it would look like.
     
     Event:
    CheckBox is derived from the ToggleButton so main event for the checkbox are Checked and Unchecked. 
     
    Add the following handler in the XAML
    <CheckBox Height="15" Margin="16,17,122,0" Name="chkA2zdotnet" VerticalAlignment="Top" Background="Cyan" BorderBrush="Blue" Foreground="Chocolate" FlowDirection="RightToLeft" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Padding="2,0,0,0"
                      Checked="chkA2zdotnet_OnChecked" Unchecked="chkA2zdotnet_OnUnchecked">A2ZDotnet.com</CheckBox>
     
     Now write the chkA2zdotnet_OnChecked and chkA2zdotnet_OnUnchecked handler for the events.
     
    protected void chkA2zdotnet_OnUnchecked(object sender, RoutedEventArgs e)
      {
         chkA2zdotnet.Background = Brushes.Cyan;
      }
     
    protected void chkA2zdotnet_OnChecked(object sender, RoutedEventArgs e)
      {
          chkA2zdotnet.Background = Brushes.LightPink;
      }
     
     
    IsChecked property is used to findout if a checkbox is checked or not as follows:
    private void button1_Click(object sender, RoutedEventArgs e)
            {
                if (chkCShaprinterview.IsChecked == true) //checked
                    MessageBox.Show("http://www.a2zdotnet.com/QuestionsList.aspx is selected");
     
                if (chkA2zdotnet.IsChecked == true) //checked
                    MessageBox.Show("chkA2zdotnet is selected");
     
                if(chkContactUs.IsChecked == false) //not checked
                    MessageBox.Show("Contact Us is not selected");
            }
      

  2.   

    CheckBox控件和RadioButton控件继承自ToggleButton类,这意味着用户可以切换它们的开与关的状态,也就是CheckBox控件的选中与未选中的状态,也可以标记为Null状态。如果在XAML中标记Null状态,XAML代码如下:    <StackPanel>
            <CheckBox IsChecked="{x:Null}"></CheckBox>
        </StackPanel>其中ToggleButton按钮还添加了一个名称为IsThreeState的属性,此属性决定用户是否可以将复选框设置为不确定状态也就是Null状态。如果IsThreeState属性设置为False则只有True和False两种选择,此属性设置为True则会有三种选择True、False和Null。示例代码如下所示。<Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <StackPanel>
            <CheckBox Name="cbox_Select" IsThreeState="True">CheckBox控件</CheckBox>
            <Button Click="Button_Click">设置CheckBox控件的IsTheeState为True</Button>
            <Button Click="Button_Click_1">设置CheckBox控件的IsThreeState为False</Button>
        </StackPanel>
    </Window>CS代码如下:        private void Button_Click(object sender, RoutedEventArgs e)
            {
                cbox_Select.IsThreeState = true;
            }        private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                cbox_Select.IsThreeState = false;
            }
     
      

  3.   

    知道了,原来还有一个Unchecked事件
                    void cbDenoise_Unchecked(object sender, RoutedEventArgs e)
            {
                bool? isSelect = this.userCtrlTargetMeasurementArea1.cbDenoise.IsChecked;            System.Windows.Forms.MessageBox.Show(isSelect.ToString());
            }