Sharana
Sharana

Reputation: 13

Check Box binding WPF

I have 3 checkboxes. Lets call them cb1,cb2 and cb3. The cb3 should be checkëd when cb1 and cb2 are checked. How do I Implement this in WPF.? I am new to WPF. Thanks in advance.

Upvotes: 1

Views: 245

Answers (2)

pete the pagan-gerbil
pete the pagan-gerbil

Reputation: 3166

If you are using a ViewModel, just add a new computed property to that.

A simple view model would look like:

public class MyVieWModel : INotifyPropertyChanged
{
    private bool _cb1Checked;
    private bool _cb2Checked;

    public bool CB1Checked
    {
        get { return _cb1Checked; }
        set
        {
            _cb1Checked = value;
            PropertyChanged(this, new PropertyChangedEventArgs("CB1Checked"));
            PropertyChanged(this, new PropertyChangedEventArgs("CB3Checked"));
        }
    }

    public bool CB2Checked
    {
        get { return _cb2Checked; }
        set
        {
            _cb2Checked = value;
            PropertyChanged(this, new PropertyChangedEventArgs("CB2Checked"));
            PropertyChanged(this, new PropertyChangedEventArgs("CB3Checked"));
        }
    }

    public bool CB3Checked
    {
        get { return _cb1Checked && _cb2Checked; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

After setting CB1Checked or CB2Checked, you need to raise the event that CB3Checked has also changed.

Your XAML would look something like (and this is from memory...):

<CheckBox IsChecked={Binding CB1Checked}" />
<CheckBox IsChecked={Binding CB2Checked}" />
<CheckBox IsChecked={Binding CB3Checked, Mode=OneWay}" />

As @wkl points out in the comments, the third checkbox should be a one-way binding since the value can't be set.

Some MVVM frameworks might make this a little easier for you, I've not used any to be able to recommend though.

Upvotes: 4

Felix D.
Felix D.

Reputation: 5093

You can do it like this for a single checkbox:

<CheckBox x:Name="cb3" IsChecked="{Binding Path=IsChecked, ElementName=cb2}" />

I assume the other checkbox is called cb2.

For multiple checkboxes I recommend a Binding in you DataContext.

<CheckBox IsChecked="{Binding Path=CB_1_Checked}" Content="CheckBox 1" />

public bool CB_1_Checked 
{
   get { return _cb_1_checked; }
   set
   {
       _cb_1_checked = value;
       OnPropertyChanged();

       //Notify that CB_3_Checked may have changed:
       OnPropertyChanged("CB_3_Checked");
   }
}

Do this for CB1 and CB2.

Add this for CB3

//Will return 'true' when both are checked (but lacks OnPropertyChanged !)
public bool CB_3_Checked => (CB_1_Checked && CB_2_Checked); 

Or with a little more options:

private bool _cb_3_checked;

public bool CB_3_Checked 
{
   get 
   {        
       if(CB_1_Checked && CB_2_Checked)
       {             
          _cb_3_checked = true; 
       }
       return _cb_3_checked;          
   }
   set
   {           
       _cb_3_checked = value;
       OnPropertyChanged();
   }
}

Read more about Bindings here.

Upvotes: 3

Related Questions