forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataGridAssist.cs
More file actions
244 lines (203 loc) · 10.6 KB
/
Copy pathDataGridAssist.cs
File metadata and controls
244 lines (203 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
namespace MaterialDesignThemes.Wpf
{
public static class DataGridAssist
{
private static DataGrid _suppressComboAutoDropDown;
public static readonly DependencyProperty AutoGeneratedCheckBoxStyleProperty = DependencyProperty
.RegisterAttached(
"AutoGeneratedCheckBoxStyle", typeof (Style), typeof (DataGridAssist),
new PropertyMetadata(default(Style), AutoGeneratedCheckBoxStylePropertyChangedCallback));
private static void AutoGeneratedCheckBoxStylePropertyChangedCallback(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((DataGrid) dependencyObject).AutoGeneratingColumn += (sender, args) =>
{
var dataGridCheckBoxColumn = args.Column as DataGridCheckBoxColumn;
if (dataGridCheckBoxColumn == null) return;
dataGridCheckBoxColumn.ElementStyle = GetAutoGeneratedCheckBoxStyle(dependencyObject);
};
}
public static void SetAutoGeneratedCheckBoxStyle(DependencyObject element, Style value)
{
element.SetValue(AutoGeneratedCheckBoxStyleProperty, value);
}
public static Style GetAutoGeneratedCheckBoxStyle(DependencyObject element)
{
return (Style) element.GetValue(AutoGeneratedCheckBoxStyleProperty);
}
public static readonly DependencyProperty AutoGeneratedEditingCheckBoxStyleProperty = DependencyProperty
.RegisterAttached(
"AutoGeneratedEditingCheckBoxStyle", typeof (Style), typeof (DataGridAssist),
new PropertyMetadata(default(Style), AutoGeneratedEditingCheckBoxStylePropertyChangedCallback));
private static void AutoGeneratedEditingCheckBoxStylePropertyChangedCallback(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((DataGrid) dependencyObject).AutoGeneratingColumn += (sender, args) =>
{
var dataGridCheckBoxColumn = args.Column as DataGridCheckBoxColumn;
if (dataGridCheckBoxColumn == null) return;
dataGridCheckBoxColumn.EditingElementStyle = GetAutoGeneratedEditingCheckBoxStyle(dependencyObject);
};
}
public static void SetAutoGeneratedEditingCheckBoxStyle(DependencyObject element, Style value)
{
element.SetValue(AutoGeneratedEditingCheckBoxStyleProperty, value);
}
public static Style GetAutoGeneratedEditingCheckBoxStyle(DependencyObject element)
{
return (Style) element.GetValue(AutoGeneratedEditingCheckBoxStyleProperty);
}
public static readonly DependencyProperty AutoGeneratedEditingTextStyleProperty = DependencyProperty
.RegisterAttached(
"AutoGeneratedEditingTextStyle", typeof (Style), typeof (DataGridAssist),
new PropertyMetadata(default(Style), AutoGeneratedEditingTextStylePropertyChangedCallback));
private static void AutoGeneratedEditingTextStylePropertyChangedCallback(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((DataGrid) dependencyObject).AutoGeneratingColumn += (sender, args) =>
{
var dataGridTextColumn = args.Column as DataGridTextColumn;
if (dataGridTextColumn == null) return;
dataGridTextColumn.EditingElementStyle = GetAutoGeneratedEditingTextStyle(dependencyObject);
};
}
public static void SetAutoGeneratedEditingTextStyle(DependencyObject element, Style value)
{
element.SetValue(AutoGeneratedEditingTextStyleProperty, value);
}
public static Style GetAutoGeneratedEditingTextStyle(DependencyObject element)
{
return (Style) element.GetValue(AutoGeneratedEditingTextStyleProperty);
}
public static readonly DependencyProperty CellPaddingProperty = DependencyProperty.RegisterAttached(
"CellPadding", typeof (Thickness), typeof (DataGridAssist),
new FrameworkPropertyMetadata(new Thickness(13, 8, 8, 8), FrameworkPropertyMetadataOptions.Inherits));
public static void SetCellPadding(DependencyObject element, Thickness value)
{
element.SetValue(CellPaddingProperty, value);
}
public static Thickness GetCellPadding(DependencyObject element)
{
return (Thickness) element.GetValue(CellPaddingProperty);
}
public static readonly DependencyProperty ColumnHeaderPaddingProperty = DependencyProperty.RegisterAttached(
"ColumnHeaderPadding", typeof (Thickness), typeof (DataGridAssist),
new FrameworkPropertyMetadata(new Thickness(8), FrameworkPropertyMetadataOptions.Inherits));
public static void SetColumnHeaderPadding(DependencyObject element, Thickness value)
{
element.SetValue(ColumnHeaderPaddingProperty, value);
}
public static Thickness GetColumnHeaderPadding(DependencyObject element)
{
return (Thickness) element.GetValue(ColumnHeaderPaddingProperty);
}
public static readonly DependencyProperty EnableEditBoxAssistProperty = DependencyProperty.RegisterAttached(
"EnableEditBoxAssist", typeof (bool), typeof (DataGridAssist),
new PropertyMetadata(default(bool), EnableCheckBoxAssistPropertyChangedCallback));
public static void SetEnableEditBoxAssist(DependencyObject element, bool value)
{
element.SetValue(EnableEditBoxAssistProperty, value);
}
public static bool GetEnableEditBoxAssist(DependencyObject element)
{
return (bool) element.GetValue(EnableEditBoxAssistProperty);
}
private static void EnableCheckBoxAssistPropertyChangedCallback(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var dataGrid = dependencyObject as DataGrid;
if (dataGrid == null) return;
if ((bool) dependencyPropertyChangedEventArgs.NewValue)
{
dataGrid.PreviewMouseLeftButtonDown += DataGridOnPreviewMouseLeftButtonDown;
dataGrid.KeyDown += DataGridOnKeyDown;
}
else
{
dataGrid.PreviewMouseLeftButtonDown -= DataGridOnPreviewMouseLeftButtonDown;
dataGrid.KeyDown -= DataGridOnKeyDown;
}
}
private static void DataGridOnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space &&
e.OriginalSource is DataGridCell cell &&
cell.IsReadOnly == false &&
cell.Column is DataGridComboBoxColumn &&
sender is DataGrid dataGrid)
{
dataGrid.BeginEdit();
}
}
private static void DataGridOnPreviewMouseLeftButtonDown(object sender,
MouseButtonEventArgs mouseButtonEventArgs)
{
var dataGrid = (DataGrid) sender;
var inputHitTest =
dataGrid.InputHitTest(mouseButtonEventArgs.GetPosition((DataGrid) sender)) as DependencyObject;
while (inputHitTest != null)
{
var dataGridCell = inputHitTest as DataGridCell;
if (dataGridCell != null && dataGrid.Equals(dataGridCell.GetVisualAncestry().OfType<DataGrid>().FirstOrDefault()))
{
if (dataGridCell.IsReadOnly) return;
ToggleButton toggleButton;
ComboBox comboBox;
if (IsDirectHitOnEditComponent(dataGridCell, mouseButtonEventArgs, out toggleButton))
{
dataGrid.CurrentCell = new DataGridCellInfo(dataGridCell);
dataGrid.BeginEdit();
toggleButton.SetCurrentValue(ToggleButton.IsCheckedProperty, !toggleButton.IsChecked);
dataGrid.CommitEdit();
mouseButtonEventArgs.Handled = true;
}
else if (IsDirectHitOnEditComponent(dataGridCell, mouseButtonEventArgs, out comboBox))
{
if (_suppressComboAutoDropDown != null) return;
dataGrid.CurrentCell = new DataGridCellInfo(dataGridCell);
dataGrid.BeginEdit();
//check again, as we move to the edit template
if (IsDirectHitOnEditComponent(dataGridCell, mouseButtonEventArgs, out comboBox))
{
_suppressComboAutoDropDown = dataGrid;
comboBox.DropDownClosed += ComboBoxOnDropDownClosed;
comboBox.IsDropDownOpen = true;
}
mouseButtonEventArgs.Handled = true;
}
return;
}
inputHitTest = (inputHitTest is Visual || inputHitTest is Visual3D)
? VisualTreeHelper.GetParent(inputHitTest)
: null;
}
}
private static void ComboBoxOnDropDownClosed(object sender, EventArgs eventArgs)
{
_suppressComboAutoDropDown.CommitEdit();
_suppressComboAutoDropDown = null;
((ComboBox)sender).DropDownClosed -= ComboBoxOnDropDownClosed;
}
private static bool IsDirectHitOnEditComponent<TControl>(ContentControl contentControl, MouseEventArgs mouseButtonEventArgs, out TControl control)
where TControl : Control
{
control = contentControl.Content as TControl;
if (control == null) return false;
var frameworkElement = VisualTreeHelper.GetChild(contentControl, 0) as FrameworkElement;
if (frameworkElement == null) return false;
var transformToAncestor = (MatrixTransform) control.TransformToAncestor(frameworkElement);
var rect = new Rect(
new Point(transformToAncestor.Value.OffsetX, transformToAncestor.Value.OffsetY),
new Size(control.ActualWidth, control.ActualHeight));
return rect.Contains(mouseButtonEventArgs.GetPosition(frameworkElement));
}
}
}