-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathContextMenuViewModel.cs
540 lines (481 loc) · 17.2 KB
/
ContextMenuViewModel.cs
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#region Copyright Syncfusion Inc. 2001 - 2016
// Copyright Syncfusion Inc. 2001 - 2016. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Syncfusion.Windows.Controls.Grid;
using System.Text.RegularExpressions;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using Syncfusion.UI.Xaml.TreeGrid;
using System.Collections;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.TreeGrid.Helpers;
using syncfusion.demoscommon.wpf;
using System.Windows.Input;
namespace syncfusion.treegriddemos.wpf
{
public class ContextMenuViewModel : EmployeeInfoViewModel
{
/// <summary>
/// Initializes a new instance of the <see cref="ContextMenuViewModel"/> class.
/// </summary>
public ContextMenuViewModel()
{
this.PersonDetails = this.CreateGenericPersonData(5, 8);
Expand = new BaseCommand(OnExpandClicked, CanExpandClicked);
SortAscending = new BaseCommand(OnSortAscendingClicked, CanSortAscending);
Collapse = new BaseCommand(OnCollapseClicked, CanCollapseClicked);
SortDescending = new BaseCommand(OnSortDescendingClicked, CanSortDescending);
ClearSorting = new BaseCommand(OnClearSortingClicked, CanClearSort);
BestFit = new DelegateCommand(OnBestFitClicked);
Cut = new DelegateCommand(OnCutClicked);
Copy = new DelegateCommand(OnCopyClicked);
Paste = new BaseCommand(OnPasteClicked, CanPaste);
Delete = new DelegateCommand(OnDeleteClicked);
AddAbove = new DelegateCommand(OnAddAboveClicked);
AddBelow = new DelegateCommand(OnAddBelowClicked);
AddAsChild = new DelegateCommand(OnAddAsChildClicked);
}
/// <summary>
/// Initializes a new instance of the <see cref="FileExplorerViewModel"/> class.
/// </summary>
/// <param name="count">The count.</param>
/// <param name="maxGenerations">The max generations.</param>
public ContextMenuViewModel(int count, int maxGenerations)
{
CreateGenericPersonData(count, maxGenerations);
}
#region ContextMenuCommands
#region Expand
private ICommand expand;
public ICommand Expand
{
get
{
return expand;
}
set
{
expand = value;
}
}
private void OnExpandClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
grid.ExpandNode(contextMenuInfo.TreeNode);
}
private bool CanExpandClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return false;
var grid = contextMenuInfo.TreeGrid;
var node = contextMenuInfo.TreeNode;
if (node.HasChildNodes)
return !node.IsExpanded;
return false;
}
#endregion
#region Collapse
private ICommand collapse;
public ICommand Collapse
{
get
{
return collapse;
}
set
{
collapse = value;
}
}
private void OnCollapseClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
var node = contextMenuInfo.TreeNode;
grid.CollapseNode(node);
}
private bool CanCollapseClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return false;
var grid = contextMenuInfo.TreeGrid;
var treeNode = contextMenuInfo.TreeNode as TreeNode;
return treeNode.IsExpanded;
}
#endregion
#region SortAscending
private ICommand sortAscending;
public ICommand SortAscending
{
get
{
return sortAscending;
}
set
{
sortAscending = value;
}
}
private void OnSortAscendingClicked(object obj)
{
var contextMenuInfo = obj as TreeGridColumnContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
var column = contextMenuInfo.Column;
if (grid.SortColumnDescriptions != null)
{
grid.SortColumnDescriptions.Clear();
grid.SortColumnDescriptions.Add(new SortColumnDescription() { ColumnName = column.MappingName, SortDirection = ListSortDirection.Ascending });
}
}
private bool CanSortAscending(object obj)
{
var contextMenuInfo = obj as TreeGridColumnContextMenuInfo;
if (contextMenuInfo == null)
return false;
var grid = contextMenuInfo.TreeGrid;
var column = contextMenuInfo.Column;
if (grid.SortColumnDescriptions != null)
{
var sortColumn = grid.SortColumnDescriptions.FirstOrDefault(x => x.ColumnName == column.MappingName);
if (sortColumn != null)
{
if ((sortColumn as SortColumnDescription).SortDirection == ListSortDirection.Ascending)
return false;
}
}
return grid.AllowSorting;
}
#endregion
#region SortDescending
private ICommand sortDescending;
public ICommand SortDescending
{
get
{
return sortDescending;
}
set
{
sortDescending = value;
}
}
private void OnSortDescendingClicked(object obj)
{
var contextMenuInfo = obj as TreeGridColumnContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
var column = contextMenuInfo.Column;
if (grid.SortColumnDescriptions != null)
{
grid.SortColumnDescriptions.Clear();
grid.SortColumnDescriptions.Add(new SortColumnDescription() { ColumnName = column.MappingName, SortDirection = ListSortDirection.Descending });
}
}
private bool CanSortDescending(object obj)
{
var contextMenuInfo = obj as TreeGridColumnContextMenuInfo;
if (contextMenuInfo == null)
return false;
var grid = contextMenuInfo.TreeGrid;
var column = contextMenuInfo.Column;
if (grid.SortColumnDescriptions != null)
{
var sortColumn = grid.SortColumnDescriptions.FirstOrDefault(x => x.ColumnName == column.MappingName);
if (sortColumn != null)
{
if ((sortColumn as SortColumnDescription).SortDirection == ListSortDirection.Descending)
return false;
}
}
return grid.AllowSorting;
}
#endregion
#region ClearSorting
private ICommand clearSorting;
public ICommand ClearSorting
{
get
{
return clearSorting;
}
set
{
clearSorting = value;
}
}
private bool CanClearSort(object obj)
{
if (obj == null)
return false;
var contextMenuInfo = obj as TreeGridColumnContextMenuInfo;
var grid = contextMenuInfo.TreeGrid;
var column = contextMenuInfo.Column;
if (grid.SortColumnDescriptions == null)
return false;
return grid.SortColumnDescriptions.Any(x => x.ColumnName == column.MappingName);
}
private void OnClearSortingClicked(object obj)
{
var contextMenuInfo = obj as TreeGridColumnContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
var column = contextMenuInfo.Column;
if (grid.SortColumnDescriptions != null && grid.SortColumnDescriptions.Any(x => x.ColumnName == column.MappingName))
grid.SortColumnDescriptions.Remove(grid.SortColumnDescriptions.FirstOrDefault(x => x.ColumnName == column.MappingName));
}
#endregion
#region BestFit
private ICommand bestFit;
public ICommand BestFit
{
get
{
return bestFit;
}
set
{
bestFit = value;
}
}
private void OnBestFitClicked(object obj)
{
var contextMenuInfo = obj as TreeGridColumnContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
var column = contextMenuInfo.Column;
column.ColumnSizer = TreeColumnSizer.Auto;
}
#endregion
#region Cut
private ICommand cut;
public ICommand Cut
{
get
{
return cut;
}
set
{
cut = value;
}
}
private void OnCutClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
grid.GridCopyOption = GridCopyOption.CutData;
grid.TreeGridCopyPaste.Cut();
}
#endregion
#region Copy
private ICommand copy;
public ICommand Copy
{
get
{
return copy;
}
set
{
copy = value;
}
}
private void OnCopyClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
grid.GridCopyOption = GridCopyOption.CopyData;
grid.TreeGridCopyPaste.Copy();
}
#endregion
#region Paste
private ICommand paste;
public ICommand Paste
{
get
{
return paste;
}
set
{
paste = value;
}
}
private bool CanPaste(object obj)
{
if (Clipboard.GetDataObject() != null && (Clipboard.GetDataObject() as DataObject).ContainsText())
return true;
return false;
}
private void OnPasteClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = (obj as TreeGridNodeContextMenuInfo).TreeGrid;
grid.TreeGridCopyPaste.Paste();
}
#endregion
#region Delete
private ICommand delete;
public ICommand Delete
{
get
{
return delete;
}
set
{
delete = value;
}
}
private void OnDeleteClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
var grid = contextMenuInfo.TreeGrid;
var node = contextMenuInfo.TreeNode;
var item = node.Item as EmployeeInfo;
if (item != null && node.Level == 0)
(grid.DataContext as ContextMenuViewModel).PersonDetails.Remove(item);
else
{
var collection = grid.View.GetPropertyAccessProvider().GetValue(node.ParentNode.Item, grid.ChildPropertyName) as IList;
collection.Remove(item);
}
}
#endregion
#region AddAbove
private ICommand addAbove;
public ICommand AddAbove
{
get
{
return addAbove;
}
set
{
addAbove = value;
}
}
private void OnAddAboveClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
Random random = new Random(123123);
var treeGrid = contextMenuInfo.TreeGrid;
var treeNode = contextMenuInfo.TreeNode;
var parentNode = treeNode.ParentNode;
var item = treeNode.Item as EmployeeInfo;
var newItem = new EmployeeInfo() { ID = EmployeeInfo._globalId++, Children = new System.Collections.ObjectModel.ObservableCollection<EmployeeInfo>(), FirstName = ContextMenuViewModel.names1[random.Next(ContextMenuViewModel.names1.GetLength(0))], LastName = ContextMenuViewModel.names2[random.Next(ContextMenuViewModel.names2.GetLength(0))] };
IList collection = null;
if (treeNode.ParentNode != null)
{
collection = treeGrid.View.GetPropertyAccessProvider().GetValue(parentNode.Item, treeGrid.ChildPropertyName) as IList;
}
else
collection = treeGrid.ItemsSource as IList;
var itemIndex = collection.IndexOf(item);
collection.Insert(itemIndex, newItem);
}
#endregion
#region AddBelow
private ICommand addBelow;
public ICommand AddBelow
{
get
{
return addBelow;
}
set
{
addBelow = value;
}
}
private void OnAddBelowClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
Random random = new Random(123123);
var treeGrid = contextMenuInfo.TreeGrid;
var treeNode = contextMenuInfo.TreeNode;
var parentNode = treeNode.ParentNode;
var item = treeNode.Item as EmployeeInfo;
var newItem = new EmployeeInfo() { ID = EmployeeInfo._globalId++, Children = new System.Collections.ObjectModel.ObservableCollection<EmployeeInfo>(), FirstName = ContextMenuViewModel.names1[random.Next(ContextMenuViewModel.names1.GetLength(0))], LastName = ContextMenuViewModel.names2[random.Next(ContextMenuViewModel.names2.GetLength(0))] };
IList collection = null;
if (treeNode.ParentNode != null)
{
collection = treeGrid.View.GetPropertyAccessProvider().GetValue(parentNode.Item, treeGrid.ChildPropertyName) as IList;
}
else
collection = treeGrid.ItemsSource as IList;
var itemIndex = collection.IndexOf(item);
collection.Insert(itemIndex + 1, newItem);
}
#endregion
#region AddAsChild
private ICommand addAsChild;
public ICommand AddAsChild
{
get
{
return addAsChild;
}
set
{
addAsChild = value;
}
}
private void OnAddAsChildClicked(object obj)
{
var contextMenuInfo = obj as TreeGridNodeContextMenuInfo;
if (contextMenuInfo == null)
return;
Random random = new Random(123123);
var treeGrid = contextMenuInfo.TreeGrid;
var treeNode = contextMenuInfo.TreeNode;
var item = treeNode.Item as EmployeeInfo;
var newItem = new EmployeeInfo() { ID = EmployeeInfo._globalId++, Children = new System.Collections.ObjectModel.ObservableCollection<EmployeeInfo>(), FirstName = ContextMenuViewModel.names1[random.Next(ContextMenuViewModel.names1.GetLength(0))], LastName = ContextMenuViewModel.names2[random.Next(ContextMenuViewModel.names2.GetLength(0))] };
var rowIndex = treeGrid.ResolveToRowIndex(treeNode);
var collection = treeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.Item, treeGrid.ChildPropertyName) as IList;
collection.Insert(collection.Count, newItem);
if (!treeNode.HasChildNodes)
{
treeGrid.ResetNode(treeNode);
treeGrid.UpdateDataRow(rowIndex);
}
}
#endregion
#endregion
}
}