3

I have 4 checkboxes one check all and the other three are subcheckboxes. I wanted to check all three subcheckboxes when I check check all and when I deselect one check box the main checkbox check all will be unchecked only.

<Window x:Class="checkboxWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525">

  <Grid Name="grdOne">
    <CheckBox Content="Check"
              Checked="checkBox1_Checked"
              Unchecked="checkBox1_Unchecked"
              Height="16"
              HorizontalAlignment="Left"
              Margin="39,23,0,0"
              Name="checkBox1"
              VerticalAlignment="Top">
    </CheckBox>
    <CheckBox Content="One"
              Unchecked="checkBox2_Unchecked"
              IsChecked="{Binding Path=Ischecked, ElementName=checkBox1, Mode=TwoWay}"
              Height="16"
              HorizontalAlignment="Left"
              Margin="117,67,0,0"
              Name="checkBox2"
              VerticalAlignment="Top" />
    <CheckBox Content="Two"
              Unchecked="checkBox3_Unchecked"
              IsChecked="{Binding Path=Ischecked, ElementName=checkBox1, Mode=TwoWay}"
              Height="16"
              HorizontalAlignment="Left"
              Margin="118,103,0,0"
              Name="checkBox3"
              VerticalAlignment="Top" />
    <CheckBox Content="Three"
              IsChecked="{Binding Path=Ischecked, ElementName=checkBox1, Mode=TwoWay}"
              Height="16"
              HorizontalAlignment="Left"
              Margin="117,145,0,0"
              Name="checkBox4"
              VerticalAlignment="Top" />
    <CheckBox Content="Four"
              IsChecked="{Binding Path=Ischecked, ElementName=checkBox1, Mode=TwoWay}"
              Height="16"
              HorizontalAlignment="Left"
              Margin="118,190,0,0"
              Name="checkBox5"
              VerticalAlignment="Top" />
  </Grid>
</Window>
 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked == true)
            {
                checkBox2.IsChecked = true;
                checkBox3.IsChecked = true;
            }
        }

        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            //if (checkBox1.IsChecked == false)
            //{
            //    checkBox2.IsChecked = false;
            //    checkBox3.IsChecked = false;
            //}
        }

        private void checkBox2_Unchecked(object sender, RoutedEventArgs e)
        {
            if (checkBox2.IsChecked == false)
            {
                checkBox1.IsChecked = false;
            }
        }

        private void checkBox3_Unchecked(object sender, RoutedEventArgs e)
        {
            if (checkBox3.IsChecked == false)
            {
                checkBox1.IsChecked = false;
            }
        }
    }
1
  • what is your problem? Commented Apr 20, 2013 at 6:04

4 Answers 4

7

My CheckBox grid looks as:

 <Grid Name="grdOne">
    <CheckBox Content="Check" Height="16" HorizontalAlignment="Left" Margin="24,44,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked" />
    <CheckBox Content="One" Height="16" HorizontalAlignment="Left" Margin="64,77,0,0" Name="checkBox2" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked" />
    <CheckBox Content="Two" Height="16" HorizontalAlignment="Left" Margin="64,99,0,0" Name="checkBox3" VerticalAlignment="Top" Unchecked="checkBox1_Unchecked" Checked="checkBox1_Checked" />
    <CheckBox Content="Three" Height="16" HorizontalAlignment="Left" Margin="64,121,0,0" Name="checkBox4" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked" />
    <CheckBox Content="Four" Height="16" HorizontalAlignment="Left" Margin="64,143,0,0" Name="checkBox5" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked" />
</Grid>

and i have two events, one for checked and another for unchecked:

bool m_bChkUpdating = false;

bool m_bUnChkUpdating = false;

private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
    CheckBox chk = (CheckBox)sender;

    if (!m_bChkUpdating)
    {
        m_bChkUpdating = true;
        switch (chk.Name)
        {
            case "checkBox1":
                checkBox2.IsChecked = true;
                checkBox3.IsChecked = true;
                checkBox4.IsChecked = true;
                checkBox5.IsChecked = true;
                break;
            default:
                //  chk.IsChecked = true;
                if (checkBox2.IsChecked == true &&
                    checkBox3.IsChecked == true &&
                    checkBox4.IsChecked == true &&
                    checkBox5.IsChecked == true)
                {
                    checkBox1.IsChecked = true;
                }
                else
                {
                    checkBox1.IsChecked = false;
                }
                break;
        }
        m_bChkUpdating = false;
    }

}

private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    if (!m_bUnChkUpdating)
    {
        m_bUnChkUpdating = true;

        switch (chk.Name)
        {
            case "checkBox1":
                checkBox2.IsChecked = false;
                checkBox3.IsChecked = false;
                checkBox4.IsChecked = false;
                checkBox5.IsChecked = false;
                break;
            default:
                // chk.IsChecked = false;
                if (checkBox2.IsChecked == false ||
                    checkBox3.IsChecked == false ||
                    checkBox4.IsChecked == false ||
                    checkBox5.IsChecked == false)
                {
                    checkBox1.IsChecked = false;
                }
                else
                {
                    checkBox1.IsChecked = true;
                }
                break;
        }

        m_bUnChkUpdating = false;
    }
}    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I only need now is when all is checked then I unchecked any one of checkbox 2 to checkbox 4 it will only uncheck the main checkbox/checkbox 1 not the other checkboxes. Thanks again for everyone's help.
6

I suggest you to use a MVVM implementation. Where the model class holds label,selection status and a collection of the subItems model. In this way you could propagate the check event to the subItems. This may be an implementation:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Checks
{
    class ChecksModel : INotifyPropertyChanged
    {
        public ChecksModel(string label, bool status)
        {
            Children = new ObservableCollection<ChecksModel>();
            Label = label;
            IsChecked = status;
        }
        public ChecksModel(string label)
            : this(label, false)
        {
        }

        public ChecksModel()
        {
            Children = new ObservableCollection<ChecksModel>();
        }
        public void AddChild(ChecksModel child)
        {
            Children.Add(child);
        }


        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        private ObservableCollection<ChecksModel> _Children;
        public ObservableCollection<ChecksModel> Children
        {
            get
            {
                return _Children;
            }
            set
            {
                _Children = value;
                OnPropertyChanged("Children");
            }
        }
        private string _Label;
        public string Label
        {
            get
            {
                return _Label;
            }
            set
            {
                _Label = value;
                OnPropertyChanged("Label");
            }

        }
        private bool _IsChecked;
        public bool IsChecked
        {
            get
            {
                return _IsChecked;
            }
            set
            {
                _IsChecked = value;
                OnPropertyChanged("IsChecked");
                CheckNodes(value);
            }
        }
        private void CheckNodes(bool value)
        {
            foreach (ChecksModel m in _Children)
            {
                m.IsChecked = value;
            }
        }
    }
}

Then in your XAML you could define a DataTemplate for the model class just created and then set an instance of the Model class as the ContentControl DataContext as follows.

    xmlns:local="clr-namespace:Checks"

    Title="MainWindow">
<Window.Resources>
    <DataTemplate 
          DataType="{x:Type local:ChecksModel}">
        <StackPanel>
            <CheckBox 
                    IsChecked="{Binding IsChecked, Mode=TwoWay}" 
                    Content="{Binding Label, Mode=OneWay}"/>
            <ItemsControl Margin="10,0,0,0" 
                     ItemsSource="{Binding Children}"/>
        </StackPanel>
    </DataTemplate>

</Window.Resources>
<Grid Name="grdOne">

    <ContentControl Content="{Binding}"/>


</Grid>

This is the Code-Bihind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections.ObjectModel;

namespace Checks
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ChecksModel model = new ChecksModel("Main");
            ChecksModel m1 = new ChecksModel("Branch");
            m1.AddChild(new ChecksModel("1st"));
            m1.AddChild(new ChecksModel("2nd"));
            model.AddChild(m1);
            model.AddChild(new ChecksModel("3rd"));
            model.AddChild(new ChecksModel("4th"));

            DataContext = model;
        }
    }
}

This is how it will look like

enter image description here

Comments

0

I think that you need CheckBox in TreeView, this article will help you:

Working with Checkboxes in the WPF TreeView

Comments

0

Is very simply with Click Event.

Here an exmaple with 2 Checkbox:

  • chbox_pres is the first Checkbox.
  • chbox_dev is the second Checkbox.
  • valor_pres and valor_dev are blobal variables to save the value of each Checkbox.

private void Chbox_Click(object sender, RoutedEventArgs e)
{
    CheckBox chk = (CheckBox)sender;

    string name = chk.Name;

    if (name == "chbox_pres")
    {
        valor_pres = chbox_pres.IsChecked ?? false;
        chbox_dev.IsChecked = !valor_pres;
        valor_dev = !valor_pres;
        Debug.WriteLine("<< pres: pres = " + valor_pres + ", dev= " + valor_dev);
    }
    else
    {
        valor_dev = chbox_dev.IsChecked ?? false;
        chbox_pres.IsChecked = !valor_dev;
        valor_pres = !valor_dev;
        Debug.WriteLine("<< dev: pres = " + valor_pres + ", dev= " + valor_dev);
    }            
}      

You can adapt to as many as you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.