Reputation: 229
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.
Upvotes: 4
Views: 19772
Reputation: 56
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.
<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;
}
}
Upvotes: 0
Reputation: 1
{
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";
}
}
Upvotes: 0
Reputation: 5059
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.
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:
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.
Edit: If I've misread this, as Blam is suggesting, and you meant that one CheckBox in particular unchecks 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.
Upvotes: 3
Reputation: 45096
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 ");
}
}
Upvotes: 5
Reputation: 32505
You can go one of two ways about this:
RadioButton
s in a groupSelector
with a customized 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 ListBox
as an example, but you can select anything that derives from Selector
.
Upvotes: 1