From 0a338c5a0f296240f466be08329b7c4d4478fec0 Mon Sep 17 00:00:00 2001 From: GF-Huang Date: Fri, 27 Nov 2020 15:22:03 +0800 Subject: [PATCH 1/4] Add attached property "DataGridAttach.ClickBlankUnselect". --- .../Controls/Attach/DataGridAttach.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs index 6b1739e6..75f684c8 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; +using System.Windows.Media; using HandyControl.Data; namespace HandyControl.Controls @@ -307,5 +308,50 @@ namespace HandyControl.Controls } private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString(); + + /// + /// 在 中点击空白地方取消选择所有 Row 和 Cell + /// + public static readonly DependencyProperty ClickBlankUnselectProperty = + DependencyProperty.RegisterAttached("ClickBlankUnselect", typeof(bool), typeof(DataGrid), new PropertyMetadata(false, ClickBlankUnselectPropertyChanged)); + + public static bool GetClickBlankUnselect(DependencyObject obj) + { + return (bool)obj.GetValue(ClickBlankUnselectProperty); + } + + public static void SetClickBlankUnselect(DependencyObject obj, bool value) + { + obj.SetValue(ClickBlankUnselectProperty, value); + } + + public static void ClickBlankUnselectPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is DataGrid dataGrid && e.NewValue is bool enabled) + { + if (enabled) + dataGrid.PreviewMouseDown += DataGrid_PreviewMouseDown; + else + dataGrid.PreviewMouseDown -= DataGrid_PreviewMouseDown; + } + } + + private static void DataGrid_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) + { + if (sender is DataGrid dataGrid) + { + var dependencyObject = e.OriginalSource as DependencyObject; + while (dependencyObject != null && !(dependencyObject is DataGridRow)) + { + dependencyObject = VisualTreeHelper.GetParent(dependencyObject); + } + + if (dependencyObject == null) + { + dataGrid.CommitEdit(); + dataGrid.UnselectAll(); + } + } + } } } From 19c16608d99cec98f4d28faf0a80c648f14b7bde Mon Sep 17 00:00:00 2001 From: GF-Huang Date: Fri, 27 Nov 2020 16:07:44 +0800 Subject: [PATCH 2/4] Fix ownerType. --- .../HandyControl_Shared/Controls/Attach/DataGridAttach.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs index 75f684c8..20859135 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs @@ -313,7 +313,7 @@ namespace HandyControl.Controls /// 在 中点击空白地方取消选择所有 Row 和 Cell /// public static readonly DependencyProperty ClickBlankUnselectProperty = - DependencyProperty.RegisterAttached("ClickBlankUnselect", typeof(bool), typeof(DataGrid), new PropertyMetadata(false, ClickBlankUnselectPropertyChanged)); + DependencyProperty.RegisterAttached("ClickBlankUnselect", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(false, ClickBlankUnselectPropertyChanged)); public static bool GetClickBlankUnselect(DependencyObject obj) { From 08967132c4157dcf04b99d1fbd0ffd6fd17c120b Mon Sep 17 00:00:00 2001 From: GF-Huang Date: Fri, 27 Nov 2020 23:34:39 +0800 Subject: [PATCH 3/4] Fix issues and rename. --- .../UserControl/Styles/DataGridDemoCtl.xaml | 2 +- .../Controls/Attach/DataGridAttach.cs | 30 +++++++------------ 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Styles/DataGridDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Styles/DataGridDemoCtl.xaml index bcd47613..e48355df 100644 --- a/src/Shared/HandyControlDemo_Shared/UserControl/Styles/DataGridDemoCtl.xaml +++ b/src/Shared/HandyControlDemo_Shared/UserControl/Styles/DataGridDemoCtl.xaml @@ -9,7 +9,7 @@ - + diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs index 20859135..1ede0679 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs @@ -312,20 +312,21 @@ namespace HandyControl.Controls /// /// 在 中点击空白地方取消选择所有 Row 和 Cell /// - public static readonly DependencyProperty ClickBlankUnselectProperty = - DependencyProperty.RegisterAttached("ClickBlankUnselect", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(false, ClickBlankUnselectPropertyChanged)); + public static readonly DependencyProperty CanUnselectRowsWithBlankAreaProperty = + DependencyProperty.RegisterAttached("CanUnselectRowsWithBlankArea", typeof(bool), typeof(DataGridAttach), + new PropertyMetadata(false, OnCanUnselectRowsWithBlankAreaChanged)); - public static bool GetClickBlankUnselect(DependencyObject obj) + public static bool GetCanUnselectRowsWithBlankArea(DependencyObject obj) { - return (bool)obj.GetValue(ClickBlankUnselectProperty); + return (bool)obj.GetValue(CanUnselectRowsWithBlankAreaProperty); } - public static void SetClickBlankUnselect(DependencyObject obj, bool value) + public static void SetCanUnselectRowsWithBlankArea(DependencyObject obj, bool value) { - obj.SetValue(ClickBlankUnselectProperty, value); + obj.SetValue(CanUnselectRowsWithBlankAreaProperty, value); } - public static void ClickBlankUnselectPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + public static void OnCanUnselectRowsWithBlankAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is DataGrid dataGrid && e.NewValue is bool enabled) { @@ -338,19 +339,10 @@ namespace HandyControl.Controls private static void DataGrid_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { - if (sender is DataGrid dataGrid) + if (sender is DataGrid dataGrid && e.OriginalSource is System.Windows.Controls.ScrollViewer) { - var dependencyObject = e.OriginalSource as DependencyObject; - while (dependencyObject != null && !(dependencyObject is DataGridRow)) - { - dependencyObject = VisualTreeHelper.GetParent(dependencyObject); - } - - if (dependencyObject == null) - { - dataGrid.CommitEdit(); - dataGrid.UnselectAll(); - } + dataGrid.CommitEdit(); + dataGrid.UnselectAll(); } } } From 93887ea1eb5d073008849f223242eca9d9e35d2b Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sat, 28 Nov 2020 12:58:23 +0800 Subject: [PATCH 4/4] Update DataGridAttach.cs --- .../Controls/Attach/DataGridAttach.cs | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs index 1ede0679..42ac2354 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/DataGridAttach.cs @@ -2,7 +2,7 @@ using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; -using System.Windows.Media; +using System.Windows.Input; using HandyControl.Data; namespace HandyControl.Controls @@ -309,35 +309,25 @@ namespace HandyControl.Controls private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString(); - /// - /// 在 中点击空白地方取消选择所有 Row 和 Cell - /// - public static readonly DependencyProperty CanUnselectRowsWithBlankAreaProperty = - DependencyProperty.RegisterAttached("CanUnselectRowsWithBlankArea", typeof(bool), typeof(DataGridAttach), - new PropertyMetadata(false, OnCanUnselectRowsWithBlankAreaChanged)); + public static readonly DependencyProperty CanUnselectAllWithBlankAreaProperty = DependencyProperty.RegisterAttached( + "CanUnselectAllWithBlankArea", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnCanUnselectAllWithBlankAreaChanged)); - public static bool GetCanUnselectRowsWithBlankArea(DependencyObject obj) + private static void OnCanUnselectAllWithBlankAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - return (bool)obj.GetValue(CanUnselectRowsWithBlankAreaProperty); - } - - public static void SetCanUnselectRowsWithBlankArea(DependencyObject obj, bool value) - { - obj.SetValue(CanUnselectRowsWithBlankAreaProperty, value); - } - - public static void OnCanUnselectRowsWithBlankAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - if (d is DataGrid dataGrid && e.NewValue is bool enabled) + if (d is DataGrid dataGrid) { - if (enabled) + if ((bool)e.NewValue) + { dataGrid.PreviewMouseDown += DataGrid_PreviewMouseDown; + } else + { dataGrid.PreviewMouseDown -= DataGrid_PreviewMouseDown; + } } } - private static void DataGrid_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) + private static void DataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if (sender is DataGrid dataGrid && e.OriginalSource is System.Windows.Controls.ScrollViewer) { @@ -345,5 +335,11 @@ namespace HandyControl.Controls dataGrid.UnselectAll(); } } + + public static void SetCanUnselectAllWithBlankArea(DependencyObject element, bool value) + => element.SetValue(CanUnselectAllWithBlankAreaProperty, value); + + public static bool GetCanUnselectAllWithBlankArea(DependencyObject element) + => (bool) element.GetValue(CanUnselectAllWithBlankAreaProperty); } }