How to Select All CheckBox of a Column by DataGrid Header CheckBox in WPF DataGrid

后端 未结 4 939
[愿得一人]
[愿得一人] 2020-12-17 22:54

I have a DataGrid with one CheckBoxColumn. In the header of that CheckBoxColumn I have added a CheckBox to Select all CheckBoxes of that Datagrid Row.

How can I achi

4条回答
  •  一生所求
    2020-12-17 23:24

    Convert your Candidate class into something like this:

    public class Candidate : DependencyObject
    {
        //CandidateID Dependency Property
        public int CandidateID
        {
            get { return (int)GetValue(CandidateIDProperty); }
            set { SetValue(CandidateIDProperty, value); }
        }
        public static readonly DependencyProperty CandidateIDProperty =
            DependencyProperty.Register("CandidateID", typeof(int), typeof(Candidate), new UIPropertyMetadata(0));
        //RegisterNo Dependency Property
        public int RegisterNo
        {
            get { return (int)GetValue(RegisterNoProperty); }
            set { SetValue(RegisterNoProperty, value); }
        }
        public static readonly DependencyProperty RegisterNoProperty =
            DependencyProperty.Register("RegisterNo", typeof(int), typeof(Candidate), new UIPropertyMetadata(0));
        //CandidateName Dependency Property
        public string CandidateName
        {
            get { return (string)GetValue(CandidateNameProperty); }
            set { SetValue(CandidateNameProperty, value); }
        }
        public static readonly DependencyProperty CandidateNameProperty =
            DependencyProperty.Register("CandidateName", typeof(string), typeof(Candidate), new UIPropertyMetadata(""));
        //BooleanFlag Dependency Property
        public bool BooleanFlag
        {
            get { return (bool)GetValue(BooleanFlagProperty); }
            set { SetValue(BooleanFlagProperty, value); }
        }
        public static readonly DependencyProperty BooleanFlagProperty =
            DependencyProperty.Register("BooleanFlag", typeof(bool), typeof(Candidate), new UIPropertyMetadata(false));
    }
    

    in MainWindow.xaml:

    
        
            
            
            
            
                
                    
                
                
                    
                        
                    
                
            
        
    
    

    in MainWindow.xaml.cs:

        public MainWindow()
        {
            DataContext = this;
            CandidateList.Add(new Candidate()
            {
                CandidateID = 1,
                CandidateName = "Jack",
                RegisterNo = 123,
                BooleanFlag = true
            });
            CandidateList.Add(new Candidate()
            {
                CandidateID = 2,
                CandidateName = "Jim",
                RegisterNo = 234,
                BooleanFlag = false
            });
            InitializeComponent();
        }
        //List Observable Collection
        private ObservableCollection _candidateList = new ObservableCollection();
        public ObservableCollection CandidateList { get { return _candidateList; } }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var item in CandidateList)
            {
                item.BooleanFlag = true;
            }
        }
        private void UnheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var item in CandidateList)
            {
                item.BooleanFlag = false;
            }
        }
    

提交回复
热议问题