C#WPF取消选中复选框 [英] C# WPF uncheck checkboxes

查看:934
本文介绍了C#WPF取消选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用微软的Visual 2013 Express中,C#和WPF。



在我的计划有六个复选框,当其中一人被选中,其他五宜得到选中。



我GOOGLE的最后两个小时,但无法找到一个解决方案,在C#初学者。在Java中,我将只写 checkbox1.setSelected(假);



我添加了一个clickevent,一检查,选中EVET在C#代码。我加了经过未选中中的.xaml,但我不知道烫得到它的工作。



希望你能帮助我:)



============== =========



我的解决办法:



感谢您的帮助。我尝试了一些随机的东西与器isChecked你的建议,并得到它的工作,很幸运



.XAML如下:

 <复选框X:NAME =CheckBox1...经过=CheckBox1_Checked/> 
<复选框X:NAME =CheckBox2...经过=CheckBox2_Checked/>
<复选框X:NAME =CheckBox3...经过=CheckBox3_Checked/>
<复选框X:NAME =CheckBox4...经过=CheckBox4_Checked/>
<复选框X:NAME =CheckBox5...经过=CheckBox5_Checked/>
<复选框X:NAME =CheckBox6...经过=CheckBox6_Checked/>为CheckBox1



C#代码:

 私人无效CheckBox1_Checked(对象发件人,RoutedEventArgs E)
{
CheckBox1.IsChecked = TRUE;
CheckBox2.IsChecked = FALSE;
CheckBox3.IsChecked = FALSE;
CheckBox4.IsChecked = FALSE;
CheckBox5.IsChecked = FALSE;
CheckBox6.IsChecked = FALSE;
}



例如。为CheckBox2:

 私人无效CheckBox2_Checked(对象发件人,RoutedEventArgs E)
{
CheckBox2.IsChecked =真正;
CheckBox1.IsChecked = FALSE;
CheckBox3.IsChecked = FALSE;
CheckBox4.IsChecked = FALSE;
CheckBox5.IsChecked = FALSE;
CheckBox6.IsChecked = FALSE;
}



那么,到底,这是一个很容易的任务来完成。


解决方案

  checkbox1.IsChecked = FALSE; 

ToggleButton.IsChecked物业



我读它为一个特殊的。如果你想有一次只有一个选择了,那么单选按钮会那样做。结果
默认情况下你不能说取消选中其他5结果
取消选中一个单选按钮。结果
这将使所有6选中。



我想结合较好,实现INotifyPropertyChanged

 私人布尔cbSecial = FALSE; 
私人布尔CB1 = FALSE;
私人布尔CB2 = FALSE;
公布尔CbSpecial
{
{返回cbSecial; }

{
如果(价值== cbSecial)回报;
cbSecial =价值;
如果(cbSpecial)
{
Cb1的= FALSE;
Cb2的= FALSE;

}
NotifyPropertyChanged(CbSpecial);
}
}
公布尔Cb1的
{
{返回CB1; }

{
如果(价值== CB1)回报;
CB1 =价值;
NotifyPropertyChanged(Cb1的);
}
}


I use MS Visual 2013 Express, C#, and WPF.

In my program there are six checkboxes, and when one of them is checked, the other five should get unchecked.

I googled the last two hours, but can't find a solution as a beginner in C#. In java, i would just write checkbox1.setSelected(false);

I added a clickevent, a checked and unchecked evet in the C# code. I added Checked and Unchecked in the .xaml, but I don't know hot to get it to work.

Hope you can help me :)

=======================

My solution:

Thank you for your help. I tried some random stuff with "IsChecked" you suggested and get it to work luckily.

.Xaml looks like:

            <CheckBox x:Name="CheckBox1" ... Checked="CheckBox1_Checked"  />
            <CheckBox x:Name="CheckBox2" ... Checked="CheckBox2_Checked"  />
            <CheckBox x:Name="CheckBox3" ... Checked="CheckBox3_Checked"  />
            <CheckBox x:Name="CheckBox4" ... Checked="CheckBox4_Checked"  />
            <CheckBox x:Name="CheckBox5" ... Checked="CheckBox5_Checked"  />
            <CheckBox x:Name="CheckBox6" ... Checked="CheckBox6_Checked"  />

C# code for CheckBox1:

    private void CheckBox1_Checked(object sender, RoutedEventArgs e)
    {
        CheckBox1.IsChecked = true;
        CheckBox2.IsChecked = false;
        CheckBox3.IsChecked = false;
        CheckBox4.IsChecked = false;
        CheckBox5.IsChecked = false;
        CheckBox6.IsChecked = false;
    }

e.g. for CheckBox2:

    private void CheckBox2_Checked(object sender, RoutedEventArgs e)
    {
        CheckBox2.IsChecked = true;
        CheckBox1.IsChecked = false;
        CheckBox3.IsChecked = false;
        CheckBox4.IsChecked = false;
        CheckBox5.IsChecked = false;
        CheckBox6.IsChecked = false;
    }

So in the end, it is a very easy task to do.

解决方案

checkbox1.IsChecked = false;

ToggleButton.IsChecked Property

I am reading it as one special that unchecks the other 5.
If you want to have just one checked at a time then RadioButton would do that.
By default you can't uncheck a RadioButton.
This would allow for all 6 unchecked.

I think binding is better and implement INotifyPropertyChanged

private Bool cbSecial = false;
private Bool cb1 = false;
private Bool cb2 = false;
public Bool CbSpecial 
{
   get { return cbSecial; }
   set 
   {
      if (value == cbSecial) return;
      cbSecial = value;
      if (cbSpecial) 
      {
          Cb1 = false;
          Cb2 = false;
          ... 
      }
      NotifyPropertyChanged("CbSpecial");
   }
}
public Bool Cb1 
{
   get { return cb1; }
   set 
   {
      if (value == cb1) return;
      cb1 = value;
      NotifyPropertyChanged("Cb1 ");
   }
}

这篇关于C#WPF取消选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆