Merge pull request #521 from GF-Huang/master

Add attached property "DataGridAttach.ClickBlankUnselect".
This commit is contained in:
NaBian authored and GitHub committed 2020-11-28 13:01:56 +08:00
commit 30e32253ed
2 files changed
+35 -1

No files matched your search

@@ -9,7 +9,7 @@
<hc:TransitioningContentControl>
<TabControl Style="{StaticResource TabControlInLine}">
<TabItem Header="{ex:Lang Key={x:Static langs:LangKeys.Common}}">
<DataGrid HeadersVisibility="All" RowHeaderWidth="60" AutoGenerateColumns="False" ItemsSource="{Binding DataList}">
<DataGrid hc:DataGridAttach.CanUnselectRowsWithBlankArea="True" HeadersVisibility="All" RowHeaderWidth="60" AutoGenerateColumns="False" ItemsSource="{Binding DataList}">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
@@ -2,6 +2,7 @@ using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using HandyControl.Data;
namespace HandyControl.Controls
@@ -307,5 +308,38 @@ namespace HandyControl.Controls
}
private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString();
public static readonly DependencyProperty CanUnselectAllWithBlankAreaProperty = DependencyProperty.RegisterAttached(
"CanUnselectAllWithBlankArea", typeof(bool), typeof(DataGridAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnCanUnselectAllWithBlankAreaChanged));
private static void OnCanUnselectAllWithBlankAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is DataGrid dataGrid)
{
if ((bool)e.NewValue)
{
dataGrid.PreviewMouseDown += DataGrid_PreviewMouseDown;
}
else
{
dataGrid.PreviewMouseDown -= DataGrid_PreviewMouseDown;
}
}
}
private static void DataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (sender is DataGrid dataGrid && e.OriginalSource is System.Windows.Controls.ScrollViewer)
{
dataGrid.CommitEdit();
dataGrid.UnselectAll();
}
}
public static void SetCanUnselectAllWithBlankArea(DependencyObject element, bool value)
=> element.SetValue(CanUnselectAllWithBlankAreaProperty, value);
public static bool GetCanUnselectAllWithBlankArea(DependencyObject element)
=> (bool) element.GetValue(CanUnselectAllWithBlankAreaProperty);
}
}