wpf DataGrid CheckBox列全选

最近在wpf项目中遇到当DataGrid的header中的checkbox选中,让该列的checkbox全选问题,为了不让程序员写自己的一堆事件,现写了一个自己的自定义控件

在DataGrid的 <DataGridTemplateColumn.HeaderTemplate> 中使用此控件即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace _UserControls
{
    public class DataGridHeaderCheckBox : CheckBox
    {
        public DataGridHeaderCheckBox()
        {
            this.Click += new System.Windows.RoutedEventHandler(DataGridHeaderCheckBox_Click);
        }

        private int thisColumnIndex = 0;

        /// <summary>
        /// 当前 CheckBox 列的索引,默认是 0
        /// </summary>
        public int ThisColumnIndex
        {
            get { return thisColumnIndex; }
            set { thisColumnIndex = value; }
        }

        /// <summary>
        /// 当header列的checkbox选中时设置本列的checkbox全选或全不选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataGridHeaderCheckBox_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            DependencyObject parent = this;
            while ((parent = VisualTreeHelper.GetParent(parent)) != null)
            {
                if (parent is DataGrid)
                {
                    List<CheckBox> checkList = null;
                    DataGrid currentDataGrid = ((DataGrid)parent);
                    foreach (var item in currentDataGrid.Items)
                    {
                        DataGridTemplateColumn templeColumn = currentDataGrid.Columns[this.thisColumnIndex] as DataGridTemplateColumn;

                        FrameworkElement fwElement = currentDataGrid.Columns[this.thisColumnIndex].GetCellContent(item);

        ////有时  fwElement 会为 NULL 原因是datagrid是默认开启了ui virtualization ,VisualTree并不是所有的控件,为了显示加速,virtualization        ////默认的只会加载一定范围的控件,不显示的控件并不加载

        ////需要修改 DataGrid 的属性  EnableColumnVirtualization="False"     EnableRowVirtualization="False" 就可以了
                        if (fwElement != null)
                        {
                            checkList = ControlManager.GetChildObjects<CheckBox>(fwElement, typeof(CheckBox));
                            foreach (CheckBox ch in checkList)
                            {
                                ch.IsChecked = this.IsChecked;
                            }
                        }
                    }
                    break;
                }
            }
        }


    }
}

xaml中的代码

<DataGridTemplateColumn Header="操作" Width="60">
                            <DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate>                                   
                                    <ctrls:DataGridHeaderCheckBox ThisColumnIndex="1" Content="操作" />
                                </DataTemplate>
                            </DataGridTemplateColumn.HeaderTemplate>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox Name="cb"></CheckBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

原文地址:https://www.cnblogs.com/renzhendewo/p/3161312.html