C# WPF 取消选中复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26681749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WPF uncheck checkboxes
提问by StefanS
I use MS Visual 2013 Express, C#, and WPF.
我使用 MS Visual 2013 Express、C# 和 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);
我在过去的两个小时里用谷歌搜索,但作为 C# 的初学者找不到解决方案。在java中,我只会写checkbox1.setSelected(false);
I added a clickevent, a checked and unchecked evet in the C# code. I added Checkedand Uncheckedin the .xaml, but I don't know hot to get it to work.
我在 C# 代码中添加了一个 clickevent,一个选中和未选中的 evet。我在 .xaml 中添加了Checked和Unchecked,但我不知道如何让它工作。
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.
感谢您的帮助。我用你建议的“IsChecked”尝试了一些随机的东西,并幸运地让它工作。
.Xaml looks like:
.Xaml 看起来像:
<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:
CheckBox1 的 C# 代码:
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:
例如对于 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.
所以最终,这是一项非常容易完成的任务。
回答by paparazzo
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.
我正在阅读它作为取消
选中其他 5 个的特殊内容。如果您只想一次选中一个,那么 RadioButton 会这样做。
默认情况下,您无法取消选中 RadioButton。
这将允许所有 6 个未选中。
I think binding is better and implement INotifyPropertyChanged
我认为绑定更好并实现 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 ");
}
}
回答by furkle
BradleyDotNET is correct - having any given CheckBox in a group uncheck the rest of them is essentially the function of a RadioButton, and you don't end up needing to do anything to implement this sort of behavior.
BradleyDotNET 是正确的 - 将组中的任何给定 CheckBox 取消选中其余的 CheckBox 本质上是 RadioButton 的功能,您最终不需要做任何事情来实现这种行为。
But, if you really wanted to use CheckBoxes instead, one way you could accomplish that is to keep reference to each of the CheckBox IsChecked value in a property within your ViewModel that you've TwoWay bound, and also keep an internal IEnumerable of your six values, like so:
但是,如果您真的想改用 CheckBoxes,那么您可以实现的一种方法是在您已绑定 TwoWay 的 ViewModel 中的属性中保留对每个 CheckBox IsChecked 值的引用,并保留您的六个内部 IEnumerable值,像这样:
private bool? checkBox1IsChecked = false;
public bool? CheckBox1IsChecked
{
get { return checkBox1IsChecked;
set
{
UncheckAllCheckBoxes();
checkBox1IsChecked = true;
}
}
public bool? CheckBox2IsChecked { get; set; }
...
// include 1 through 6 here
private List<bool?> yourCheckStates = new List<bool> { checkBox1IsChecked, ... };
You could use something like this every time the status changes on one of them:
每次其中之一的状态发生变化时,您都可以使用类似的方法:
private void UncheckAllCheckBoxes()
{
foreach (bool? cbIsChecked in yourCheckStates)
{
cb.IsChecked = false;
}
}
And then set the value of the one that's just been changed to true. Each time a checkbox is checked, the other five will always be unchecked.
然后将刚刚更改的那个值设置为true。每次选中一个复选框时,其他五个将始终未选中。
Edit: If I've misread this, as Blam is suggesting, and you meant that one CheckBox in particularunchecks all the others, then only include the call to UncheckAllCheckBoxes()within the property for that specific CheckBox's IsChecked value, and add a line to set that one special CheckBox's IsChecked to false in all the other setters.
编辑:如果我误读了这一点,正如 Blam 所建议的那样,并且您的意思是一个 CheckBox 特别取消选中所有其他复选框,那么只UncheckAllCheckBoxes()在属性中包含对该特定 CheckBox 的 IsChecked 值的调用,并添加一行来设置在所有其他设置器中,一个特殊的 CheckBox 的 IsChecked 为 false。
回答by m-y
You can go one of two ways about this:
您可以采用以下两种方式之一:
RadioButtons in a groupSelectorwith a customizedItemTemplate
RadioButton在一组Selector带有定制的ItemTemplate
RadioButtons:
单选按钮:
<RadioButton GroupName="MyGrouping" Content="Option 1" IsChecked="True"/>
<RadioButton GroupName="MyGrouping" Content="Option 2" />
<RadioButton GroupName="MyGrouping" Content="Option 3" />
Selector:
选择器:
<ListBox ItemsSource="{Binding Path=MyOptions}">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" />
</DataTemplate>
</Listbox.ItemTemplate>
</ListBox>
Note: I selected ListBoxas an example, but you can select anything that derives from Selector.
注意:我选择ListBox作为示例,但您可以选择任何源自Selector.
回答by Mujahid Hussain
{
if (CheckBox1.Checked)
{Label1.Text = "Checked Apple"; }
if (CheckBox1.Checked == false)
{
Label1.Text = "Unchecked Apple";
}
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox2.Checked)
{ Label1.Text = "Checked Banana"; }
if (CheckBox2.Checked == false)
{
Label1.Text = "Unchecked Banana";
}
}
回答by Nate Hammond
You might find this useful. I had a "Logging Level" wpf menu that I wanted only 1 selection active at a time (i.e. uncheck all the other menu items when selected). My trick was to use the FindName() method and a "tag" option in the xaml (to generate the names for a nice for loop). This way I avoided having a bunch of "=false" lines, and just a single shared function call for each form item.
您可能会发现这很有用。我有一个“日志级别”wpf 菜单,我希望一次只激活 1 个选项(即,在选中时取消选中所有其他菜单项)。我的技巧是在 xaml 中使用 FindName() 方法和“标记”选项(为一个好的 for 循环生成名称)。通过这种方式,我避免了一堆“=false”行,并且每个表单项只有一个共享函数调用。
<MenuItem Header="Logging">
<!-- let's use the cool TAG propery to store the debugLevel code. We don't use it, but the idea is cool -->
<MenuItem Tag="0" Name="mnuLogging0" Header="None" IsCheckable="True" Checked="mnuLogging_Checked" />
<MenuItem Tag="1" Name="mnuLogging1" Header="Normal" IsCheckable="True" Checked="mnuLogging_Checked" />
<MenuItem Tag="2" Name="mnuLogging2" Header="Debug Level 1" IsCheckable="True" Checked="mnuLogging_Checked" />
<MenuItem Tag="3" Name="mnuLogging3" Header="Debug Level 2" IsCheckable="True" Checked="mnuLogging_Checked" />
</MenuItem>
And the function to uncheck the other options, along with bonus code for saving the settings is:
取消选中其他选项的功能以及保存设置的奖励代码是:
private void mnuLogging_Checked(object sender, RoutedEventArgs e) {
int LogLevel = Convert.ToInt32( ((MenuItem)sender).Tag.ToString());
Properties.Settings.Default["LogLevel"] = LogLevel.ToString();
Properties.Settings.Default.Save();
// Uncheck everybody but me (as determine by "Tag")
for (int i=0; i < 4; i++) {
MenuItem item = Main.FindName("mnuLogging"+i) as MenuItem;
if (i != LogLevel) item.IsChecked = false;
}
}

