WPF多个复选框选中/取消选中

时间:2013-04-20 06:03:44

标签: c# wpf xaml

我有4个复选框,一个检查全部,其他三个是子检查框。当我选中全部检查时,我想检查所有三个子检查框,当我取消选中一个复选框时,主复选框检查全部将仅取消选中。

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

4 个答案:

答案 0 :(得分:5)

我的CheckBox grid看起来像:

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

我有两个活动,一个用于checked,另一个用于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;
    }
}    

答案 1 :(得分:4)

我建议你使用MVVM实现。模型类包含标签,选择状态和subItems模型的集合。通过这种方式,您可以将check事件传播到subItems。这可能是一个实现:

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

然后在您的XAML中,您可以为刚刚创建的模型类定义DataTemplate,然后将Model类的实例设置为ContentControl DataContext,如下所示。

    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>

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

这就是它的样子

enter image description here

答案 2 :(得分:0)

我认为您需要CheckBox TreeView,本文将为您提供帮助:

Working with Checkboxes in the WPF TreeView

答案 3 :(得分:0)

非常简单的点击事件。

这里有一个带有2个复选框的示例:

  • chbox_pres 是第一个复选框
  • chbox_dev 是第二个复选框
  • valor_pres valor_dev 是用于保存每个复选框的值的全局变量。

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

您可以根据需要进行调整。