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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM