翼度科技»论坛 编程开发 .net 查看内容

WPF DataGrid开箱即用支持全部勾选的DataGridCheckBoxColumn

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
  1. 本文由 飞羽流星(Flithor/毛茸茸松鼠先生/Mr.Squirrel.Downy)原创,欢迎分享转载,但禁止以原创二次发布<br>原文地址:<a href="https://www.cnblogs.com/Flithor/p/17877473.html" target="_blank" rel="noopener">https://www.cnblogs.com/Flithor/p/17877473.html</a>
复制代码
以往在WPF的DataGrid上实现实时勾选交互非常麻烦,要用DataGridTemplateColumn写样式还要写一些后端代码,还有绑定和转换器,或者处理事件。
但是现在都不需要了,使用开箱即用的DataGridCheckAllColumn,简化你的勾选代码实现!
它支持列表筛选变化反馈,支持虚拟化,支持单项勾选变化的更新反馈。
效果预览:

而且非常简单好用:
  1. <DataGrid.Columns>
  2.    
  3.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  4.    
  5.     <DataGridTextColumn Binding="{Binding EntityName}" />
  6. </DataGrid.Columns>
复制代码
  1. <strong>注意!</strong>
  2. 如果你在DataGrid上设置了<strong>VirtualizingPanel.IsVirtualizing="True"</strong>应用虚拟化
  3. 需要同时设置<strong>VirtualizingPanel.VirtualizationMode="Standard"</strong>,避免出现绑定实例错误的Bug
复制代码
DataGridCheckAllColumn类的实现:
  1. // 代码作者: 飞羽流星(Flithor/Mr.Squirrel.Downy/毛茸茸松鼠先生)// 开源许可: MIT// =======警告=======// 使用开源代码的风险自负using System;using System.Collections.Generic;using System.ComponentModel;using System.Globalization;using System.Linq;using System.Reflection;using System.Runtime.Remoting.Messaging;using System.Windows;using System.Windows.Controls;using System.Windows.Controls.Primitives;using System.Windows.Data;using System.Windows.Media;using Expression = System.Linq.Expressions.Expression;namespace Flithor_Codes{    ///     /// 一个支持勾选所有的DataGrid列    ///     public class DataGridCheckAllColumn : DataGridBoundColumn    {        #region 私有字段        //列头中的CheckBox        private readonly CheckBox checkAllCheckBox;        //所属DataGrid控件        private DataGrid ownerDatagrid;        //所属DataGrid控件的列表版本,如果版本号改变则说明列表改变        private Func getInnerEnumeratorVersion;        //缓存的列表版本号        private int cachedInnerVersion;        //CheckBox的默认样式        private static Style _defaultElementStyle;        #endregion        #region 初始化控件        public static Style DefaultElementStyle        {            get            {<DataGrid.Columns>
  2.    
  3.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  4.    
  5.     <DataGridTextColumn Binding="{Binding EntityName}" />
  6. </DataGrid.Columns>if (_defaultElementStyle == null)<DataGrid.Columns>
  7.    
  8.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  9.    
  10.     <DataGridTextColumn Binding="{Binding EntityName}" />
  11. </DataGrid.Columns>{<DataGrid.Columns>
  12.    
  13.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  14.    
  15.     <DataGridTextColumn Binding="{Binding EntityName}" />
  16. </DataGrid.Columns>    var style = new Style(typeof(CheckBox))<DataGrid.Columns>
  17.    
  18.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  19.    
  20.     <DataGridTextColumn Binding="{Binding EntityName}" />
  21. </DataGrid.Columns>    {<DataGrid.Columns>
  22.    
  23.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  24.    
  25.     <DataGridTextColumn Binding="{Binding EntityName}" />
  26. </DataGrid.Columns>        Setters =<DataGrid.Columns>
  27.    
  28.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  29.    
  30.     <DataGridTextColumn Binding="{Binding EntityName}" />
  31. </DataGrid.Columns>        {<DataGrid.Columns>
  32.    
  33.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  34.    
  35.     <DataGridTextColumn Binding="{Binding EntityName}" />
  36. </DataGrid.Columns>            new Setter(UIElement.FocusableProperty, false),<DataGrid.Columns>
  37.    
  38.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  39.    
  40.     <DataGridTextColumn Binding="{Binding EntityName}" />
  41. </DataGrid.Columns>            new Setter(CheckBox.HorizontalAlignmentProperty, HorizontalAlignment.Center),<DataGrid.Columns>
  42.    
  43.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  44.    
  45.     <DataGridTextColumn Binding="{Binding EntityName}" />
  46. </DataGrid.Columns>            new Setter(CheckBox.VerticalAlignmentProperty, VerticalAlignment.Center)<DataGrid.Columns>
  47.    
  48.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  49.    
  50.     <DataGridTextColumn Binding="{Binding EntityName}" />
  51. </DataGrid.Columns>        }<DataGrid.Columns>
  52.    
  53.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  54.    
  55.     <DataGridTextColumn Binding="{Binding EntityName}" />
  56. </DataGrid.Columns>    };<DataGrid.Columns>
  57.    
  58.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  59.    
  60.     <DataGridTextColumn Binding="{Binding EntityName}" />
  61. </DataGrid.Columns>    style.Seal();<DataGrid.Columns>
  62.    
  63.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  64.    
  65.     <DataGridTextColumn Binding="{Binding EntityName}" />
  66. </DataGrid.Columns>    _defaultElementStyle = style;<DataGrid.Columns>
  67.    
  68.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  69.    
  70.     <DataGridTextColumn Binding="{Binding EntityName}" />
  71. </DataGrid.Columns>}<DataGrid.Columns>
  72.    
  73.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  74.    
  75.     <DataGridTextColumn Binding="{Binding EntityName}" />
  76. </DataGrid.Columns>return _defaultElementStyle;            }        }        static DataGridCheckAllColumn()        {            //重写单元格元素默认样式            ElementStyleProperty.OverrideMetadata(typeof(DataGridCheckAllColumn), new FrameworkPropertyMetadata(DefaultElementStyle));            //使列默认只读            IsReadOnlyProperty.OverrideMetadata(typeof(DataGridCheckAllColumn), new FrameworkPropertyMetadata(true));            //不允许重排此列            CanUserReorderProperty.OverrideMetadata(typeof(DataGridCheckAllColumn), new FrameworkPropertyMetadata(false));            //不允许修改此列宽度            CanUserResizeProperty.OverrideMetadata(typeof(DataGridCheckAllColumn), new FrameworkPropertyMetadata(false));            //不允许点击列头排序项目            CanUserSortProperty.OverrideMetadata(typeof(DataGridCheckAllColumn), new FrameworkPropertyMetadata(false));        }        public DataGridCheckAllColumn()        {            //设置列头            Header = checkAllCheckBox = new CheckBox();        }        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)        {            if (ownerDatagrid != null) return;            ownerDatagrid = GetParentDataGrid();            if (ownerDatagrid == null) return;            InitInnerVersionDetect(ownerDatagrid.Items);            ((INotifyPropertyChanged)ownerDatagrid.Items).PropertyChanged += OnPropertyChanged;            //如果DataGrid当前已有项目,则初始化绑定            checkAllCheckBox.IsEnabled = ownerDatagrid.Items.Count > 0;            if (checkAllCheckBox.IsEnabled)<DataGrid.Columns>
  77.    
  78.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  79.    
  80.     <DataGridTextColumn Binding="{Binding EntityName}" />
  81. </DataGrid.Columns>ResetCheckCurrentAllBinding();        }        //寻找所属DataGrid控件(如果还没初始化完毕,可能返回空值)        private DataGrid GetParentDataGrid()        {            DependencyObject elment = checkAllCheckBox;            do            {<DataGrid.Columns>
  82.    
  83.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  84.    
  85.     <DataGridTextColumn Binding="{Binding EntityName}" />
  86. </DataGrid.Columns>elment = VisualTreeHelper.GetParent(elment);            }            while (elment != null && !(elment is DataGrid));            return elment as DataGrid;        }        #endregion        #region 构建单元格元素(方法的功能不言自明)        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)        {            return GenerateCheckBox(false, cell, dataItem);        }        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)        {            return GenerateCheckBox(true, cell, dataItem);        }        private CheckBox GenerateCheckBox(bool isEditing, DataGridCell cell, object dataItem)        {            var checkBox = new CheckBox();            ApplyStyle(isEditing, checkBox);            ApplyBinding(dataItem, checkBox);            return checkBox;        }        private void ApplyBinding(object dataItem, CheckBox checkBox)        {            var binding = CloneBinding(Binding, dataItem);            if (binding is Binding newBinding)            {<DataGrid.Columns>
  87.    
  88.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  89.    
  90.     <DataGridTextColumn Binding="{Binding EntityName}" />
  91. </DataGrid.Columns>newBinding.Mode = BindingMode.TwoWay;<DataGrid.Columns>
  92.    
  93.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  94.    
  95.     <DataGridTextColumn Binding="{Binding EntityName}" />
  96. </DataGrid.Columns>newBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;            }            BindingOperations.ClearBinding(checkBox, CheckBox.IsCheckedProperty);            checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);        }        internal void ApplyStyle(bool isEditing, FrameworkElement element)        {            Style style = PickStyle(isEditing);            if (style != null)            {<DataGrid.Columns>
  97.    
  98.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  99.    
  100.     <DataGridTextColumn Binding="{Binding EntityName}" />
  101. </DataGrid.Columns>element.Style = style;            }        }        private Style PickStyle(bool isEditing)        {            Style style = isEditing ? EditingElementStyle : ElementStyle;            if (isEditing && (style == null))            {<DataGrid.Columns>
  102.    
  103.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  104.    
  105.     <DataGridTextColumn Binding="{Binding EntityName}" />
  106. </DataGrid.Columns>style = ElementStyle;            }            return style;        }        #endregion        #region 更新绑定        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)        {            if (ownerDatagrid == null || e.PropertyName != nameof(ownerDatagrid.Items.Count))<DataGrid.Columns>
  107.    
  108.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  109.    
  110.     <DataGridTextColumn Binding="{Binding EntityName}" />
  111. </DataGrid.Columns>return;            //如果项目数量发生改变意味着列表更新了            if (ownerDatagrid.Items.Count == 0)            {<DataGrid.Columns>
  112.    
  113.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  114.    
  115.     <DataGridTextColumn Binding="{Binding EntityName}" />
  116. </DataGrid.Columns>//如果列表空了,那就移除绑定并禁用列头勾选控件<DataGrid.Columns>
  117.    
  118.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  119.    
  120.     <DataGridTextColumn Binding="{Binding EntityName}" />
  121. </DataGrid.Columns>BindingOperations.ClearBinding(checkAllCheckBox, CheckBox.IsCheckedProperty);<DataGrid.Columns>
  122.    
  123.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  124.    
  125.     <DataGridTextColumn Binding="{Binding EntityName}" />
  126. </DataGrid.Columns>checkAllCheckBox.IsEnabled = false;            }            else            {<DataGrid.Columns>
  127.    
  128.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  129.    
  130.     <DataGridTextColumn Binding="{Binding EntityName}" />
  131. </DataGrid.Columns>//否则基于当前列表显示项更新绑定<DataGrid.Columns>
  132.    
  133.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  134.    
  135.     <DataGridTextColumn Binding="{Binding EntityName}" />
  136. </DataGrid.Columns>ResetCheckCurrentAllBinding();<DataGrid.Columns>
  137.    
  138.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  139.    
  140.     <DataGridTextColumn Binding="{Binding EntityName}" />
  141. </DataGrid.Columns>checkAllCheckBox.IsEnabled = true;            }        }        private void ResetCheckCurrentAllBinding()        {            //如果列表版本号改变则更新,否则无需更新            if (ownerDatagrid == null || !InnerVersionChanged()) return;            var checkAllBinding = new MultiBinding            {<DataGrid.Columns>
  142.    
  143.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  144.    
  145.     <DataGridTextColumn Binding="{Binding EntityName}" />
  146. </DataGrid.Columns>Converter = AllBoolStatusConverter.Default,<DataGrid.Columns>
  147.    
  148.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  149.    
  150.     <DataGridTextColumn Binding="{Binding EntityName}" />
  151. </DataGrid.Columns>Mode = BindingMode.TwoWay            };            //基于所有列表显示项创建绑定            var currentItems = ownerDatagrid.Items.OfType().ToList();            foreach (var item in currentItems)            {<DataGrid.Columns>
  152.    
  153.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  154.    
  155.     <DataGridTextColumn Binding="{Binding EntityName}" />
  156. </DataGrid.Columns>checkAllBinding.Bindings.Add(CloneBinding((Binding)Binding, item));            }            //清除旧绑定            BindingOperations.ClearBinding(checkAllCheckBox, CheckBox.IsCheckedProperty);            checkAllCheckBox.SetBinding(CheckBox.IsCheckedProperty, checkAllBinding);        }        //创建获取内部列表版本号的委托        private void InitInnerVersionDetect(ItemCollection itemCollection)        {            //Timestamp属性是内部列表的版本号标识,用于表示列表变更的次数            var collectionTimestampProerty = itemCollection.GetType()<DataGrid.Columns>
  157.    
  158.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  159.    
  160.     <DataGridTextColumn Binding="{Binding EntityName}" />
  161. </DataGrid.Columns>.GetProperty("Timestamp", BindingFlags.Instance | BindingFlags.NonPublic);            //使用Linq的Expression来构建访问Timestamp属性的委托方法            getInnerEnumeratorVersion = Expression.Lambda(Expression.Property(<DataGrid.Columns>
  162.    
  163.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  164.    
  165.     <DataGridTextColumn Binding="{Binding EntityName}" />
  166. </DataGrid.Columns>Expression.Constant(itemCollection),<DataGrid.Columns>
  167.    
  168.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  169.    
  170.     <DataGridTextColumn Binding="{Binding EntityName}" />
  171. </DataGrid.Columns>collectionTimestampProerty)).Compile();        }        //检查内部列表是否发生了更新        private bool InnerVersionChanged()        {            var currentInnerVersion = getInnerEnumeratorVersion.Invoke();            if (currentInnerVersion != cachedInnerVersion)            {<DataGrid.Columns>
  172.    
  173.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  174.    
  175.     <DataGridTextColumn Binding="{Binding EntityName}" />
  176. </DataGrid.Columns>cachedInnerVersion = currentInnerVersion;<DataGrid.Columns>
  177.    
  178.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  179.    
  180.     <DataGridTextColumn Binding="{Binding EntityName}" />
  181. </DataGrid.Columns>return true;            }            return false;        }        //基于已有绑定对象创建一个副本,使用指定的数据源        private static BindingBase CloneBinding(BindingBase bindingBase, object source)        {            switch (bindingBase)            {<DataGrid.Columns>
  182.    
  183.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  184.    
  185.     <DataGridTextColumn Binding="{Binding EntityName}" />
  186. </DataGrid.Columns>case Binding binding:<DataGrid.Columns>
  187.    
  188.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  189.    
  190.     <DataGridTextColumn Binding="{Binding EntityName}" />
  191. </DataGrid.Columns>    var resultBinding = new Binding<DataGrid.Columns>
  192.    
  193.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  194.    
  195.     <DataGridTextColumn Binding="{Binding EntityName}" />
  196. </DataGrid.Columns>    {<DataGrid.Columns>
  197.    
  198.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  199.    
  200.     <DataGridTextColumn Binding="{Binding EntityName}" />
  201. </DataGrid.Columns>        Source = source,<DataGrid.Columns>
  202.    
  203.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  204.    
  205.     <DataGridTextColumn Binding="{Binding EntityName}" />
  206. </DataGrid.Columns>        AsyncState = binding.AsyncState,<DataGrid.Columns>
  207.    
  208.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  209.    
  210.     <DataGridTextColumn Binding="{Binding EntityName}" />
  211. </DataGrid.Columns>        BindingGroupName = binding.BindingGroupName,<DataGrid.Columns>
  212.    
  213.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  214.    
  215.     <DataGridTextColumn Binding="{Binding EntityName}" />
  216. </DataGrid.Columns>        BindsDirectlyToSource = binding.BindsDirectlyToSource,<DataGrid.Columns>
  217.    
  218.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  219.    
  220.     <DataGridTextColumn Binding="{Binding EntityName}" />
  221. </DataGrid.Columns>        Converter = binding.Converter,<DataGrid.Columns>
  222.    
  223.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  224.    
  225.     <DataGridTextColumn Binding="{Binding EntityName}" />
  226. </DataGrid.Columns>        ConverterCulture = binding.ConverterCulture,<DataGrid.Columns>
  227.    
  228.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  229.    
  230.     <DataGridTextColumn Binding="{Binding EntityName}" />
  231. </DataGrid.Columns>        ConverterParameter = binding.ConverterParameter,<DataGrid.Columns>
  232.    
  233.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  234.    
  235.     <DataGridTextColumn Binding="{Binding EntityName}" />
  236. </DataGrid.Columns>        //ElementName = binding.ElementName,<DataGrid.Columns>
  237.    
  238.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  239.    
  240.     <DataGridTextColumn Binding="{Binding EntityName}" />
  241. </DataGrid.Columns>        FallbackValue = binding.FallbackValue,<DataGrid.Columns>
  242.    
  243.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  244.    
  245.     <DataGridTextColumn Binding="{Binding EntityName}" />
  246. </DataGrid.Columns>        IsAsync = binding.IsAsync,<DataGrid.Columns>
  247.    
  248.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  249.    
  250.     <DataGridTextColumn Binding="{Binding EntityName}" />
  251. </DataGrid.Columns>        Mode = binding.Mode,<DataGrid.Columns>
  252.    
  253.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  254.    
  255.     <DataGridTextColumn Binding="{Binding EntityName}" />
  256. </DataGrid.Columns>        NotifyOnSourceUpdated = binding.NotifyOnSourceUpdated,<DataGrid.Columns>
  257.    
  258.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  259.    
  260.     <DataGridTextColumn Binding="{Binding EntityName}" />
  261. </DataGrid.Columns>        NotifyOnTargetUpdated = binding.NotifyOnTargetUpdated,<DataGrid.Columns>
  262.    
  263.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  264.    
  265.     <DataGridTextColumn Binding="{Binding EntityName}" />
  266. </DataGrid.Columns>        NotifyOnValidationError = binding.NotifyOnValidationError,<DataGrid.Columns>
  267.    
  268.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  269.    
  270.     <DataGridTextColumn Binding="{Binding EntityName}" />
  271. </DataGrid.Columns>        Path = binding.Path,<DataGrid.Columns>
  272.    
  273.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  274.    
  275.     <DataGridTextColumn Binding="{Binding EntityName}" />
  276. </DataGrid.Columns>        //RelativeSource = binding.RelativeSource,<DataGrid.Columns>
  277.    
  278.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  279.    
  280.     <DataGridTextColumn Binding="{Binding EntityName}" />
  281. </DataGrid.Columns>        StringFormat = binding.StringFormat,<DataGrid.Columns>
  282.    
  283.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  284.    
  285.     <DataGridTextColumn Binding="{Binding EntityName}" />
  286. </DataGrid.Columns>        TargetNullValue = binding.TargetNullValue,<DataGrid.Columns>
  287.    
  288.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  289.    
  290.     <DataGridTextColumn Binding="{Binding EntityName}" />
  291. </DataGrid.Columns>        UpdateSourceExceptionFilter = binding.UpdateSourceExceptionFilter,<DataGrid.Columns>
  292.    
  293.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  294.    
  295.     <DataGridTextColumn Binding="{Binding EntityName}" />
  296. </DataGrid.Columns>        UpdateSourceTrigger = binding.UpdateSourceTrigger,<DataGrid.Columns>
  297.    
  298.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  299.    
  300.     <DataGridTextColumn Binding="{Binding EntityName}" />
  301. </DataGrid.Columns>        ValidatesOnDataErrors = binding.ValidatesOnDataErrors,<DataGrid.Columns>
  302.    
  303.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  304.    
  305.     <DataGridTextColumn Binding="{Binding EntityName}" />
  306. </DataGrid.Columns>        ValidatesOnExceptions = binding.ValidatesOnExceptions,<DataGrid.Columns>
  307.    
  308.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  309.    
  310.     <DataGridTextColumn Binding="{Binding EntityName}" />
  311. </DataGrid.Columns>        XPath = binding.XPath,<DataGrid.Columns>
  312.    
  313.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  314.    
  315.     <DataGridTextColumn Binding="{Binding EntityName}" />
  316. </DataGrid.Columns>    };<DataGrid.Columns>
  317.    
  318.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  319.    
  320.     <DataGridTextColumn Binding="{Binding EntityName}" />
  321. </DataGrid.Columns>    foreach (var validationRule in binding.ValidationRules)<DataGrid.Columns>
  322.    
  323.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  324.    
  325.     <DataGridTextColumn Binding="{Binding EntityName}" />
  326. </DataGrid.Columns>    {<DataGrid.Columns>
  327.    
  328.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  329.    
  330.     <DataGridTextColumn Binding="{Binding EntityName}" />
  331. </DataGrid.Columns>        resultBinding.ValidationRules.Add(validationRule);<DataGrid.Columns>
  332.    
  333.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  334.    
  335.     <DataGridTextColumn Binding="{Binding EntityName}" />
  336. </DataGrid.Columns>    }<DataGrid.Columns>
  337.    
  338.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  339.    
  340.     <DataGridTextColumn Binding="{Binding EntityName}" />
  341. </DataGrid.Columns>    return resultBinding;<DataGrid.Columns>
  342.    
  343.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  344.    
  345.     <DataGridTextColumn Binding="{Binding EntityName}" />
  346. </DataGrid.Columns>case MultiBinding multiBinding:<DataGrid.Columns>
  347.    
  348.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  349.    
  350.     <DataGridTextColumn Binding="{Binding EntityName}" />
  351. </DataGrid.Columns>    var resultMultiBinding = new MultiBinding<DataGrid.Columns>
  352.    
  353.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  354.    
  355.     <DataGridTextColumn Binding="{Binding EntityName}" />
  356. </DataGrid.Columns>    {<DataGrid.Columns>
  357.    
  358.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  359.    
  360.     <DataGridTextColumn Binding="{Binding EntityName}" />
  361. </DataGrid.Columns>        BindingGroupName = multiBinding.BindingGroupName,<DataGrid.Columns>
  362.    
  363.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  364.    
  365.     <DataGridTextColumn Binding="{Binding EntityName}" />
  366. </DataGrid.Columns>        Converter = multiBinding.Converter,<DataGrid.Columns>
  367.    
  368.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  369.    
  370.     <DataGridTextColumn Binding="{Binding EntityName}" />
  371. </DataGrid.Columns>        ConverterCulture = multiBinding.ConverterCulture,<DataGrid.Columns>
  372.    
  373.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  374.    
  375.     <DataGridTextColumn Binding="{Binding EntityName}" />
  376. </DataGrid.Columns>        ConverterParameter = multiBinding.ConverterParameter,<DataGrid.Columns>
  377.    
  378.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  379.    
  380.     <DataGridTextColumn Binding="{Binding EntityName}" />
  381. </DataGrid.Columns>        FallbackValue = multiBinding.FallbackValue,<DataGrid.Columns>
  382.    
  383.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  384.    
  385.     <DataGridTextColumn Binding="{Binding EntityName}" />
  386. </DataGrid.Columns>        Mode = multiBinding.Mode,<DataGrid.Columns>
  387.    
  388.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  389.    
  390.     <DataGridTextColumn Binding="{Binding EntityName}" />
  391. </DataGrid.Columns>        NotifyOnSourceUpdated = multiBinding.NotifyOnSourceUpdated,<DataGrid.Columns>
  392.    
  393.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  394.    
  395.     <DataGridTextColumn Binding="{Binding EntityName}" />
  396. </DataGrid.Columns>        NotifyOnTargetUpdated = multiBinding.NotifyOnTargetUpdated,<DataGrid.Columns>
  397.    
  398.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  399.    
  400.     <DataGridTextColumn Binding="{Binding EntityName}" />
  401. </DataGrid.Columns>        NotifyOnValidationError = multiBinding.NotifyOnValidationError,<DataGrid.Columns>
  402.    
  403.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  404.    
  405.     <DataGridTextColumn Binding="{Binding EntityName}" />
  406. </DataGrid.Columns>        StringFormat = multiBinding.StringFormat,<DataGrid.Columns>
  407.    
  408.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  409.    
  410.     <DataGridTextColumn Binding="{Binding EntityName}" />
  411. </DataGrid.Columns>        TargetNullValue = multiBinding.TargetNullValue,<DataGrid.Columns>
  412.    
  413.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  414.    
  415.     <DataGridTextColumn Binding="{Binding EntityName}" />
  416. </DataGrid.Columns>        UpdateSourceExceptionFilter = multiBinding.UpdateSourceExceptionFilter,<DataGrid.Columns>
  417.    
  418.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  419.    
  420.     <DataGridTextColumn Binding="{Binding EntityName}" />
  421. </DataGrid.Columns>        UpdateSourceTrigger = multiBinding.UpdateSourceTrigger,<DataGrid.Columns>
  422.    
  423.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  424.    
  425.     <DataGridTextColumn Binding="{Binding EntityName}" />
  426. </DataGrid.Columns>        ValidatesOnDataErrors = multiBinding.ValidatesOnDataErrors,<DataGrid.Columns>
  427.    
  428.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  429.    
  430.     <DataGridTextColumn Binding="{Binding EntityName}" />
  431. </DataGrid.Columns>        ValidatesOnExceptions = multiBinding.ValidatesOnDataErrors,<DataGrid.Columns>
  432.    
  433.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  434.    
  435.     <DataGridTextColumn Binding="{Binding EntityName}" />
  436. </DataGrid.Columns>    };<DataGrid.Columns>
  437.    
  438.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  439.    
  440.     <DataGridTextColumn Binding="{Binding EntityName}" />
  441. </DataGrid.Columns>    foreach (var validationRule in multiBinding.ValidationRules)<DataGrid.Columns>
  442.    
  443.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  444.    
  445.     <DataGridTextColumn Binding="{Binding EntityName}" />
  446. </DataGrid.Columns>    {<DataGrid.Columns>
  447.    
  448.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  449.    
  450.     <DataGridTextColumn Binding="{Binding EntityName}" />
  451. </DataGrid.Columns>        resultMultiBinding.ValidationRules.Add(validationRule);<DataGrid.Columns>
  452.    
  453.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  454.    
  455.     <DataGridTextColumn Binding="{Binding EntityName}" />
  456. </DataGrid.Columns>    }<DataGrid.Columns>
  457.    
  458.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  459.    
  460.     <DataGridTextColumn Binding="{Binding EntityName}" />
  461. </DataGrid.Columns>    foreach (var childBinding in multiBinding.Bindings)<DataGrid.Columns>
  462.    
  463.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  464.    
  465.     <DataGridTextColumn Binding="{Binding EntityName}" />
  466. </DataGrid.Columns>    {<DataGrid.Columns>
  467.    
  468.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  469.    
  470.     <DataGridTextColumn Binding="{Binding EntityName}" />
  471. </DataGrid.Columns>        resultMultiBinding.Bindings.Add(CloneBinding(childBinding, source));<DataGrid.Columns>
  472.    
  473.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  474.    
  475.     <DataGridTextColumn Binding="{Binding EntityName}" />
  476. </DataGrid.Columns>    }<DataGrid.Columns>
  477.    
  478.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  479.    
  480.     <DataGridTextColumn Binding="{Binding EntityName}" />
  481. </DataGrid.Columns>    return resultMultiBinding;<DataGrid.Columns>
  482.    
  483.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  484.    
  485.     <DataGridTextColumn Binding="{Binding EntityName}" />
  486. </DataGrid.Columns>case PriorityBinding priorityBinding:<DataGrid.Columns>
  487.    
  488.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  489.    
  490.     <DataGridTextColumn Binding="{Binding EntityName}" />
  491. </DataGrid.Columns>    var resultPriorityBinding = new PriorityBinding<DataGrid.Columns>
  492.    
  493.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  494.    
  495.     <DataGridTextColumn Binding="{Binding EntityName}" />
  496. </DataGrid.Columns>    {<DataGrid.Columns>
  497.    
  498.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  499.    
  500.     <DataGridTextColumn Binding="{Binding EntityName}" />
  501. </DataGrid.Columns>        BindingGroupName = priorityBinding.BindingGroupName,<DataGrid.Columns>
  502.    
  503.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  504.    
  505.     <DataGridTextColumn Binding="{Binding EntityName}" />
  506. </DataGrid.Columns>        FallbackValue = priorityBinding.FallbackValue,<DataGrid.Columns>
  507.    
  508.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  509.    
  510.     <DataGridTextColumn Binding="{Binding EntityName}" />
  511. </DataGrid.Columns>        StringFormat = priorityBinding.StringFormat,<DataGrid.Columns>
  512.    
  513.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  514.    
  515.     <DataGridTextColumn Binding="{Binding EntityName}" />
  516. </DataGrid.Columns>        TargetNullValue = priorityBinding.TargetNullValue,<DataGrid.Columns>
  517.    
  518.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  519.    
  520.     <DataGridTextColumn Binding="{Binding EntityName}" />
  521. </DataGrid.Columns>    };<DataGrid.Columns>
  522.    
  523.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  524.    
  525.     <DataGridTextColumn Binding="{Binding EntityName}" />
  526. </DataGrid.Columns>    foreach (var childBinding in priorityBinding.Bindings)<DataGrid.Columns>
  527.    
  528.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  529.    
  530.     <DataGridTextColumn Binding="{Binding EntityName}" />
  531. </DataGrid.Columns>    {<DataGrid.Columns>
  532.    
  533.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  534.    
  535.     <DataGridTextColumn Binding="{Binding EntityName}" />
  536. </DataGrid.Columns>        resultPriorityBinding.Bindings.Add(CloneBinding(childBinding, source));<DataGrid.Columns>
  537.    
  538.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  539.    
  540.     <DataGridTextColumn Binding="{Binding EntityName}" />
  541. </DataGrid.Columns>    }<DataGrid.Columns>
  542.    
  543.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  544.    
  545.     <DataGridTextColumn Binding="{Binding EntityName}" />
  546. </DataGrid.Columns>    return resultPriorityBinding;<DataGrid.Columns>
  547.    
  548.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  549.    
  550.     <DataGridTextColumn Binding="{Binding EntityName}" />
  551. </DataGrid.Columns>default:<DataGrid.Columns>
  552.    
  553.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  554.    
  555.     <DataGridTextColumn Binding="{Binding EntityName}" />
  556. </DataGrid.Columns>    throw new NotSupportedException("Failed to clone binding");            }        }        ///         /// 用于合并所有bool值到一个值的多值转换器        ///         private class AllBoolStatusConverter : IMultiValueConverter        {            public static readonly AllBoolStatusConverter Default = new AllBoolStatusConverter();            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)            {<DataGrid.Columns>
  557.    
  558.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  559.    
  560.     <DataGridTextColumn Binding="{Binding EntityName}" />
  561. </DataGrid.Columns>if (values.Length == 0 || values.OfType().Count() != values.Length)<DataGrid.Columns>
  562.    
  563.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  564.    
  565.     <DataGridTextColumn Binding="{Binding EntityName}" />
  566. </DataGrid.Columns>    return false;<DataGrid.Columns>
  567.    
  568.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  569.    
  570.     <DataGridTextColumn Binding="{Binding EntityName}" />
  571. </DataGrid.Columns>// 检查所有项是否和第一项相同<DataGrid.Columns>
  572.    
  573.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  574.    
  575.     <DataGridTextColumn Binding="{Binding EntityName}" />
  576. </DataGrid.Columns>var firstStatus = values.First();<DataGrid.Columns>
  577.    
  578.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  579.    
  580.     <DataGridTextColumn Binding="{Binding EntityName}" />
  581. </DataGrid.Columns>foreach (var value in values)<DataGrid.Columns>
  582.    
  583.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  584.    
  585.     <DataGridTextColumn Binding="{Binding EntityName}" />
  586. </DataGrid.Columns>{<DataGrid.Columns>
  587.    
  588.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  589.    
  590.     <DataGridTextColumn Binding="{Binding EntityName}" />
  591. </DataGrid.Columns>    //如果有不同就返回null,表示第三态<DataGrid.Columns>
  592.    
  593.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  594.    
  595.     <DataGridTextColumn Binding="{Binding EntityName}" />
  596. </DataGrid.Columns>    if (!Equals(value, firstStatus))<DataGrid.Columns>
  597.    
  598.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  599.    
  600.     <DataGridTextColumn Binding="{Binding EntityName}" />
  601. </DataGrid.Columns>        return null;<DataGrid.Columns>
  602.    
  603.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  604.    
  605.     <DataGridTextColumn Binding="{Binding EntityName}" />
  606. </DataGrid.Columns>}<DataGrid.Columns>
  607.    
  608.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  609.    
  610.     <DataGridTextColumn Binding="{Binding EntityName}" />
  611. </DataGrid.Columns>return firstStatus;            }            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)            {<DataGrid.Columns>
  612.    
  613.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  614.    
  615.     <DataGridTextColumn Binding="{Binding EntityName}" />
  616. </DataGrid.Columns>//如果汇总的值发生变化则更新所有项目<DataGrid.Columns>
  617.    
  618.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  619.    
  620.     <DataGridTextColumn Binding="{Binding EntityName}" />
  621. </DataGrid.Columns>var res = new object[targetTypes.Length];<DataGrid.Columns>
  622.    
  623.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  624.    
  625.     <DataGridTextColumn Binding="{Binding EntityName}" />
  626. </DataGrid.Columns>for (int i = 0; i < res.Length; i++)<DataGrid.Columns>
  627.    
  628.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  629.    
  630.     <DataGridTextColumn Binding="{Binding EntityName}" />
  631. </DataGrid.Columns>    res[i] = value;<DataGrid.Columns>
  632.    
  633.     <fc:DataGridCheckAllColumn Binding="{Binding IsChecked}" />
  634.    
  635.     <DataGridTextColumn Binding="{Binding EntityName}" />
  636. </DataGrid.Columns>return res;            }        }        #endregion    }}
复制代码



来源:https://www.cnblogs.com/Flithor/archive/2023/12/05/17877473.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具