-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelGrid.Elements.cs
More file actions
386 lines (319 loc) · 11.5 KB
/
ExcelGrid.Elements.cs
File metadata and controls
386 lines (319 loc) · 11.5 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using NetExtender.Utilities.Core;
namespace NetExtender.UserInterface.WindowsPresentation.ExcelGrid
{
public partial class ExcelGrid
{
protected void RefreshIfRequired(Boolean force)
{
if (!force && ItemsSource is INotifyCollectionChanged)
{
return;
}
View?.Refresh();
}
protected void SelectAll()
{
SelectionCell = new ExcelCell(Columns - 1, Rows - 1);
CurrentCell = new ExcelCell(0, 0);
ScrollIntoView(CurrentCell);
}
protected virtual FrameworkElement? CreateDisplayControl(ExcelCell cell)
{
if (Operator?.CreateCellDescriptor(cell) is not { } descriptor || ControlFactory.FactoryDisplayControl(descriptor, IsReadOnly) is not { } element)
{
return null;
}
element.DataContext = descriptor.BindingSource;
element.SourceUpdated += (_, _) => CurrentCellSourceUpdated(cell);
return element;
}
protected virtual FrameworkElement? CreateEditControl(ExcelCell cell)
{
if (Operator?.CreateCellDescriptor(cell) is not { } descriptor || ControlFactory.FactoryEditControl(descriptor) is not { } element)
{
return null;
}
element.DataContext = descriptor.BindingSource;
element.SourceUpdated += (_, _) => CurrentCellSourceUpdated(cell);
return element;
}
protected virtual void AddInserterRow(Int32 rows)
{
if (SheetGrid is not { } grid)
{
return;
}
if (!CanInsertRows || AddItemHeader is null)
{
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
RowGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
return;
}
grid.RowDefinitions.Add(new RowDefinition { Height = DefaultRowHeight });
RowGrid.RowDefinitions.Add(new RowDefinition { Height = DefaultRowHeight });
TextBlock cell = new TextBlock
{
Text = AddItemHeader,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
};
Border border = new Border
{
Background = Brushes.Transparent,
BorderBrush = HeaderBorderBrush,
BorderThickness = new Thickness(1, 0, 1, 1),
Margin = new Thickness(0, 0, 0, 0)
};
border.MouseLeftButtonDown += AddItemCellMouseLeftButtonDown;
Grid.SetRow(border, rows);
cell.Padding = new Thickness(4, 2, 4, 2);
Grid.SetRow(cell, rows);
RowGrid.Children.Add(cell);
RowGrid.Children.Add(border);
}
protected virtual void InsertDisplayControl(ExcelCell cell)
{
if (SheetGrid is not { } grid || CreateDisplayControl(cell) is not { } control)
{
return;
}
SetElementPosition(control, cell);
if (CellInsertionIndex > grid.Children.Count)
{
grid.Children.Add(control);
}
else
{
grid.Children.Insert(CellInsertionIndex++, control);
}
Map.Cells.Add(cell.GetHashCode(), control);
}
protected virtual Boolean OpenComboBoxControl()
{
if (CurrentEditControl is not ComboBox combobox)
{
return false;
}
combobox.IsDropDownOpen = true;
combobox.Focus();
return true;
}
protected virtual void ShowEditControl()
{
RemoveEditControl();
ExcelCell cell = CurrentCell;
if (IsReadOnly || cell.Row >= Rows || cell.Column >= Columns)
{
return;
}
if (CurrentEditControl is not null)
{
return;
}
EditingCells = SelectedCells.ToList();
if ((CurrentEditControl = CreateEditControl(cell)) is not { } edit)
{
return;
}
if (edit is TextBox editor)
{
editor.Visibility = Visibility.Hidden;
editor.PreviewKeyDown += TextEditorPreviewKeyDown;
}
Grid.SetColumn(edit, CurrentCell.Column);
Grid.SetRow(edit, CurrentCell.Row);
SheetGrid?.Children.Add(edit);
if (edit.Visibility == Visibility.Visible)
{
edit.Focus();
}
}
protected virtual Boolean ShowTextBoxEditControl()
{
if (CurrentEditControl is null)
{
ShowEditControl();
}
if (CurrentEditControl is not TextBox editor)
{
return false;
}
editor.Visibility = Visibility.Visible;
editor.Focus();
editor.CaretIndex = editor.Text.Length;
editor.SelectAll();
return true;
}
private void RemoveEditControl()
{
if (CurrentEditControl is not { } control)
{
return;
}
if (control is TextBox editor)
{
editor.PreviewKeyDown -= TextEditorPreviewKeyDown;
}
SheetGrid?.Children.Remove(control);
CurrentEditControl = null;
Focus();
}
protected FrameworkElement? GetColumnElement(Int32 column)
{
return Map.Columns.TryGetValue(column, out FrameworkElement? element) ? element : null;
}
protected FrameworkElement? GetRowElement(Int32 row)
{
return Map.Rows.TryGetValue(row, out FrameworkElement? element) ? element : null;
}
protected Object GetColumnHeader(Int32 column)
{
return ItemsInRows && column >= 0 && column < PropertyDefinitions.Count && PropertyDefinitions[column].Header is { } header ? header : ExcelCell.ToColumn(column);
}
protected Object GetRowHeader(Int32 row)
{
return ItemsInColumns && row < RowDefinitions.Count && RowDefinitions[row].Header is { } header ? header : ExcelCell.ToRow(row);
}
protected FrameworkElement? GetElement(ExcelCell cell)
{
return Map.Cells.TryGetValue(cell.GetHashCode(), out FrameworkElement? element) ? element is Border border ? border.Child as FrameworkElement : element : null;
}
protected static void SetElementPosition(UIElement element, ExcelCell cell)
{
if (element is null)
{
throw new ArgumentNullException(nameof(element));
}
Grid.SetColumn(element, cell.Column);
Grid.SetRow(element, cell.Row);
}
protected Boolean HandleAutoInsert(ExcelCell cell)
{
if (!AutoInsert || !CanInsert)
{
return false;
}
if (cell.Row < Rows && cell.Column < Columns)
{
return false;
}
ExcelCell current = cell;
if (cell.Row >= Rows)
{
Int32 index = Rows;
Operator?.InsertRows(index, 1);
View?.Refresh();
index = FindViewIndex(index);
current = new ExcelCell(cell.Column, index);
}
if (cell.Column >= Columns)
{
Int32 index = Columns;
Operator?.InsertColumns(index, 1);
View?.Refresh();
index = FindViewIndex(index);
current = new ExcelCell(index, cell.Row);
}
SelectionCell = current;
CurrentCell = current;
ScrollIntoView(current);
return true;
}
protected virtual void InsertColumns()
{
if (Operator is not { } @operator)
{
return;
}
Int32 from = Math.Min(CurrentCell.Column, SelectionCell.Column);
Int32 to = Math.Max(CurrentCell.Column, SelectionCell.Column);
@operator.InsertColumns(from, to - from + 1);
RefreshIfRequired(true);
}
protected virtual void InsertRows()
{
if (Operator is not { } @operator)
{
return;
}
Int32 from = Math.Min(CurrentCell.Row, SelectionCell.Row);
Int32 to = Math.Max(CurrentCell.Row, SelectionCell.Row);
@operator.InsertRows(from, to - from + 1);
RefreshIfRequired(false);
}
protected virtual void DeleteColumns()
{
DeleteColumns(true);
}
protected virtual void DeleteColumns(Boolean scroll)
{
if (Operator is not { } @operator)
{
return;
}
Int32 from = Math.Min(CurrentCell.Column, SelectionCell.Column);
Int32 to = Math.Max(CurrentCell.Column, SelectionCell.Column);
@operator.DeleteColumns(from, to - from + 1);
RefreshIfRequired(true);
Int32 columns = Columns;
Int32 maximum = columns > 0 ? columns - 1 : 0;
if (CurrentCell.Column > maximum)
{
//TODO: is another cell (already changed)?
CurrentCell = new ExcelCell(maximum, CurrentCell.Row);
}
if (SelectionCell.Column > maximum)
{
SelectionCell = new ExcelCell(maximum, SelectionCell.Row);
}
if (scroll)
{
ScrollIntoView(CurrentCell);
}
}
protected virtual void DeleteRows()
{
DeleteRows(true);
}
protected virtual void DeleteRows(Boolean scroll)
{
if (Operator is not { } @operator)
{
return;
}
Int32 from = Math.Min(CurrentCell.Row, SelectionCell.Row);
Int32 to = Math.Max(CurrentCell.Row, SelectionCell.Row);
@operator.DeleteRows(from, to - from + 1);
RefreshIfRequired(false);
Int32 rows = Rows;
Int32 maximum = rows > 0 ? rows - 1 : 0;
if (CurrentCell.Row > maximum)
{
CurrentCell = new ExcelCell(CurrentCell.Column, maximum);
}
if (SelectionCell.Row > maximum)
{
SelectionCell = new ExcelCell(SelectionCell.Column, maximum);
}
if (scroll)
{
ScrollIntoView(CurrentCell);
}
}
public virtual void Clear()
{
foreach (ExcelCell cell in SelectedCells)
{
TrySet(cell, TryGet(cell, out _) && Operator?.GetPropertyType(cell) is { } type ? ReflectionUtilities.Default(type, false) : null);
}
}
}
}