C # WPF uncheck checkboxes

I am using MS Visual 2013 Express, C # and WPF.

There are six checkboxes in my program, and when one of them is checked, the other five should be unchecked.

I've searched google for the last two hours but can't seem to find a solution as a newbie in C #. In java I will just writecheckbox1.setSelected(false);

I added a clickevent element checked and unchecked evet to C # code. I added Checked

and Unchecked

in .xaml, but I do not know how to make it work.

Hope you can help me :)

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

My decision:

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

.Xaml looks like this:

            <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;
    }

      

eg. 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, this is a very simple task.

+3


source to share


5 answers


checkbox1.IsChecked = false;

      

ToggleButton.IsChecked Property

I read it as one of the ad hoc ones that overrides the other 5.
If you only want one checked at a time, then RadioButton will do that.
By default, you cannot clear the RadioButton checkbox.
This would allow all 6 untested.



I believe binding is better and implements 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 ");
   }
}

      

+5


source


BradleyDotNET is correct - if any given CheckBox in the group disable all others, it is essentially a RadioButton function and you don't need to do anything to implement this behavior.

But, if you really wanted to use CheckBoxes, one way you could do this is to store a reference to each of the CheckBox IsChecked values ​​in a property in the ViewModel that you bound to the TwoWay, and also store an internal IEnumerable of your six values. eg:

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 can use something like this on every status change on one of them:



private void UncheckAllCheckBoxes()
{
    foreach (bool? cbIsChecked in yourCheckStates)
    {
        cb.IsChecked = false;
    }
}

      

And then set the value you just changed to true. Each time the checkbox is checked, the other five will always be unchecked.

Edit: If I misunderstood this as Blam suggests and you meant that one CheckBox in particular disables all others, then only include a call UncheckAllCheckBoxes()

to the property for that particular CheckBox IsChecked value and add a line to set that one the special CheckBox IsChecked is false in all other setters.

+2


source


You can do one of two ways:

  • RadioButton

    in Group
  • Selector

    with a tuned ItemTemplate


radio buttons:

<RadioButton GroupName="MyGrouping" Content="Option 1" IsChecked="True"/>
<RadioButton GroupName="MyGrouping" Content="Option 2" />
<RadioButton GroupName="MyGrouping" Content="Option 3" />

      


Choice:

<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 chose as an example ListBox

, but you can choose whatever comes from Selector

.

+1


source


 {
    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";
    }
}

      

0


source


You may find this helpful. I had a wpf "Logging Level" ("Logging Level") menu where I only wanted one active selection at a time (ie, uncheck all other menu items if selected). My trick was to use the FindName () method and "tag" in the xaml (to generate names for the nice loop for the loop). Thus, I avoided having a bunch of "= false" lines and just one call to a common function for each form element.

<MenuItem Header="Logging">
  <!-- let 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 of unchecking other parameters along with a bonus code to save the settings:

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;
  }
}

      

0


source







All Articles