This repository has been archived by the owner on Nov 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
SharpTreeView.cs
736 lines (634 loc) · 21.7 KB
/
SharpTreeView.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace ICSharpCode.TreeView
{
public class SharpTreeView : ListView
{
static SharpTreeView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SharpTreeView),
new FrameworkPropertyMetadata(typeof(SharpTreeView)));
SelectionModeProperty.OverrideMetadata(typeof(SharpTreeView),
new FrameworkPropertyMetadata(SelectionMode.Extended));
AlternationCountProperty.OverrideMetadata(typeof(SharpTreeView),
new FrameworkPropertyMetadata(2));
DefaultItemContainerStyleKey =
new ComponentResourceKey(typeof(SharpTreeView), "DefaultItemContainerStyleKey");
VirtualizingStackPanel.VirtualizationModeProperty.OverrideMetadata(typeof(SharpTreeView),
new FrameworkPropertyMetadata(VirtualizationMode.Recycling));
RegisterCommands();
}
public static ResourceKey DefaultItemContainerStyleKey { get; private set; }
public SharpTreeView()
{
SetResourceReference(ItemContainerStyleProperty, DefaultItemContainerStyleKey);
}
public static readonly DependencyProperty CanDragAndDropProperty =
DependencyProperty.Register("CanDragAndDrop", typeof(bool), typeof(SharpTreeView), new PropertyMetadata(true));
public bool CanDragAndDrop {
get { return (bool)GetValue(CanDragAndDropProperty); }
set { SetValue(CanDragAndDropProperty, value); }
}
public static readonly DependencyProperty RootProperty =
DependencyProperty.Register("Root", typeof(SharpTreeNode), typeof(SharpTreeView));
public SharpTreeNode Root
{
get { return (SharpTreeNode)GetValue(RootProperty); }
set { SetValue(RootProperty, value); }
}
public static readonly DependencyProperty ShowRootProperty =
DependencyProperty.Register("ShowRoot", typeof(bool), typeof(SharpTreeView),
new FrameworkPropertyMetadata(true));
public bool ShowRoot
{
get { return (bool)GetValue(ShowRootProperty); }
set { SetValue(ShowRootProperty, value); }
}
public static readonly DependencyProperty ShowRootExpanderProperty =
DependencyProperty.Register("ShowRootExpander", typeof(bool), typeof(SharpTreeView),
new FrameworkPropertyMetadata(false));
public bool ShowRootExpander
{
get { return (bool)GetValue(ShowRootExpanderProperty); }
set { SetValue(ShowRootExpanderProperty, value); }
}
public static readonly DependencyProperty AllowDropOrderProperty =
DependencyProperty.Register("AllowDropOrder", typeof(bool), typeof(SharpTreeView));
public bool AllowDropOrder
{
get { return (bool)GetValue(AllowDropOrderProperty); }
set { SetValue(AllowDropOrderProperty, value); }
}
public static readonly DependencyProperty ShowLinesProperty =
DependencyProperty.Register("ShowLines", typeof(bool), typeof(SharpTreeView),
new FrameworkPropertyMetadata(true));
public bool ShowLines
{
get { return (bool)GetValue(ShowLinesProperty); }
set { SetValue(ShowLinesProperty, value); }
}
public static bool GetShowAlternation(DependencyObject obj)
{
return (bool)obj.GetValue(ShowAlternationProperty);
}
public static void SetShowAlternation(DependencyObject obj, bool value)
{
obj.SetValue(ShowAlternationProperty, value);
}
public static readonly DependencyProperty ShowAlternationProperty =
DependencyProperty.RegisterAttached("ShowAlternation", typeof(bool), typeof(SharpTreeView),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == RootProperty ||
e.Property == ShowRootProperty ||
e.Property == ShowRootExpanderProperty) {
Reload();
}
}
TreeFlattener flattener;
bool updatesLocked;
void Reload()
{
if (flattener != null) {
flattener.Stop();
}
if (Root != null) {
if (!(ShowRoot && ShowRootExpander)) {
Root.IsExpanded = true;
}
flattener = new TreeFlattener(Root, ShowRoot);
flattener.CollectionChanged += flattener_CollectionChanged;
this.ItemsSource = flattener;
}
}
void flattener_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Deselect nodes that are being hidden, if any remain in the tree
if (e.Action == NotifyCollectionChangedAction.Remove && Items.Count > 0) {
List<SharpTreeNode> selectedOldItems = null;
foreach (SharpTreeNode node in e.OldItems) {
if (node.IsSelected) {
if (selectedOldItems == null)
selectedOldItems = new List<SharpTreeNode>();
selectedOldItems.Add(node);
}
}
if (!updatesLocked && selectedOldItems != null) {
var list = SelectedItems.Cast<SharpTreeNode>().Except(selectedOldItems).ToList();
UpdateFocusedNode(list, Math.Max(0, e.OldStartingIndex - 1));
}
}
}
void UpdateFocusedNode(List<SharpTreeNode> newSelection, int topSelectedIndex)
{
if (updatesLocked) return;
SetSelectedItems(newSelection ?? Enumerable.Empty<SharpTreeNode>());
if (SelectedItem == null) {
SelectedIndex = topSelectedIndex;
if (SelectedItem != null && IsKeyboardFocusWithin)
FocusNode((SharpTreeNode)SelectedItem);
}
}
protected override void ClearContainerForItemOverride(DependencyObject element, object item) {
var item2 = element as SharpTreeViewItem;
if (item2 != null) {
var nv = item2.NodeView;
if (nv != null)
nv.DataContext = null;
item2.ClearValue(DataContextProperty);
}
base.ClearContainerForItemOverride(element, item);
}
protected override DependencyObject GetContainerForItemOverride()
{
return new SharpTreeViewItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is SharpTreeViewItem;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
SharpTreeViewItem container = element as SharpTreeViewItem;
container.ParentTreeView = this;
// Make sure that the line renderer takes into account the new bound data
if (container.NodeView != null && container.NodeView.LinesRenderer != null) {
container.NodeView.LinesRenderer.InvalidateVisual();
}
}
bool doNotScrollOnExpanding;
/// <summary>
/// Handles the node expanding event in the tree view.
/// This method gets called only if the node is in the visible region (a SharpTreeNodeView exists).
/// </summary>
internal void HandleExpanding(SharpTreeNode node)
{
if (doNotScrollOnExpanding)
return;
SharpTreeNode lastVisibleChild = node;
while (true) {
SharpTreeNode tmp = lastVisibleChild.Children.LastOrDefault(c => c.IsVisible);
if (tmp != null) {
lastVisibleChild = tmp;
} else {
break;
}
}
if (lastVisibleChild != node) {
Debug.Assert(flattener != null);
if (flattener == null)
return;
// A new scrollViewer.ViewportHeight value is available at the earliest at Render prio.
// If there are many empty lines, it will equal the number of visible items (eg. 5,
// but viewport can show eg. 10).
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => {
var itemsPerPage = STVUTILS.GetItemsPerPage(this, 0);
int nodeIndex = flattener.IndexOf(node);
int lastVisibleChildIndex = flattener.IndexOf(lastVisibleChild);
Debug.Assert(nodeIndex >= 0 && lastVisibleChildIndex >= 0);
if (itemsPerPage > 0 && nodeIndex >= 0 && lastVisibleChildIndex >= 0) {
int lastIndex = Math.Min(lastVisibleChildIndex, nodeIndex + itemsPerPage - 1);
ScrollIntoView(Items[lastIndex]);
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => ScrollIntoView(node)));
}
}));
}
}
static class STVUTILS {
static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject {
int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
for (int i = 0; i < childrenCount; i++) {
var child = VisualTreeHelper.GetChild(obj, i);
if (child is T res)
return res;
res = FindVisualChild<T>(child);
if (res != null)
return res;
}
return null;
}
public static ScrollViewer TryGetScrollViewer(ListBox lb) => FindVisualChild<ScrollViewer>(lb);
public static int GetItemsPerPage(ListBox lb, int defaultValue) {
var scrollViewer = TryGetScrollViewer(lb);
if (scrollViewer == null)
return defaultValue;
return (int)Math.Max(1, Math.Floor(scrollViewer.ViewportHeight));
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
SharpTreeViewItem container = e.OriginalSource as SharpTreeViewItem;
if (container != null && container.Node != null) switch (e.Key) {
case Key.Left:
if (container != null && ItemsControl.ItemsControlFromItemContainer(container) == this) {
if (container.Node.IsExpanded) {
container.Node.IsExpanded = false;
} else if (container.Node.Parent != null) {
this.FocusNode(container.Node.Parent);
}
e.Handled = true;
}
break;
case Key.Right:
if (container != null && ItemsControl.ItemsControlFromItemContainer(container) == this) {
if (!container.Node.IsExpanded && container.Node.ShowExpander) {
container.Node.IsExpanded = true;
} else if (container.Node.Children.Count > 0) {
// jump to first child:
container.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
}
e.Handled = true;
}
break;
case Key.Return:
case Key.Space:
if (container != null && Keyboard.Modifiers == ModifierKeys.None && this.SelectedItems.Count == 1 && this.SelectedItem == container.Node) {
container.Node.ActivateItem(e);
}
break;
case Key.Add:
if (container != null && ItemsControl.ItemsControlFromItemContainer(container) == this) {
container.Node.IsExpanded = true;
e.Handled = true;
}
break;
case Key.Subtract:
if (container != null && ItemsControl.ItemsControlFromItemContainer(container) == this) {
container.Node.IsExpanded = false;
e.Handled = true;
}
break;
case Key.Multiply:
if (container != null && ItemsControl.ItemsControlFromItemContainer(container) == this) {
container.Node.IsExpanded = true;
ExpandRecursively(container.Node);
e.Handled = true;
}
break;
}
if (!e.Handled)
base.OnKeyDown(e);
}
void ExpandRecursively(SharpTreeNode node)
{
if (node.CanExpandRecursively) {
node.IsExpanded = true;
foreach (SharpTreeNode child in node.Children) {
ExpandRecursively(child);
}
}
}
/// <summary>
/// Scrolls the specified node in view and sets keyboard focus on it.
/// </summary>
public void FocusNode(SharpTreeNode node)
{
if (node == null)
throw new ArgumentNullException("node");
ScrollIntoView(node);
// WPF's ScrollIntoView() uses the same if/dispatcher construct, so we call OnFocusItem() after the item was brought into view.
if (this.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) {
OnFocusItem(node);
} else {
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new DispatcherOperationCallback(this.OnFocusItem), node);
}
}
public void ScrollIntoView(SharpTreeNode node)
{
if (node == null)
throw new ArgumentNullException("node");
doNotScrollOnExpanding = true;
foreach (SharpTreeNode ancestor in node.Ancestors())
ancestor.IsExpanded = true;
doNotScrollOnExpanding = false;
base.ScrollIntoView(node);
}
object OnFocusItem(object item)
{
FrameworkElement element = this.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
if (element != null) {
element.Focus();
}
return null;
}
#region Track selection
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
foreach (SharpTreeNode node in e.RemovedItems) {
node.IsSelected = false;
}
foreach (SharpTreeNode node in e.AddedItems) {
node.IsSelected = true;
}
base.OnSelectionChanged(e);
}
#endregion
#region Drag and Drop
protected override void OnDragEnter(DragEventArgs e)
{
OnDragOver(e);
}
protected override void OnDragOver(DragEventArgs e)
{
e.Effects = DragDropEffects.None;
if (Root != null && !ShowRoot) {
e.Handled = true;
Root.CanDrop(e, Root.Children.Count);
}
}
protected override void OnDrop(DragEventArgs e)
{
e.Effects = DragDropEffects.None;
if (Root != null && !ShowRoot) {
e.Handled = true;
Root.InternalDrop(e, Root.Children.Count);
}
}
internal void HandleDragEnter(SharpTreeViewItem item, DragEventArgs e)
{
HandleDragOver(item, e);
}
internal void HandleDragOver(SharpTreeViewItem item, DragEventArgs e)
{
HidePreview();
var target = GetDropTarget(item, e);
if (target != null) {
e.Handled = true;
ShowPreview(target.Item, target.Place);
}
}
internal void HandleDrop(SharpTreeViewItem item, DragEventArgs e)
{
try {
HidePreview();
var target = GetDropTarget(item, e);
if (target != null) {
e.Handled = true;
target.Node.InternalDrop(e, target.Index);
}
} catch (Exception ex) {
Debug.WriteLine(ex.ToString());
throw;
}
}
internal void HandleDragLeave(SharpTreeViewItem item, DragEventArgs e)
{
HidePreview();
e.Handled = true;
}
class DropTarget
{
public SharpTreeViewItem Item;
public DropPlace Place;
public double Y;
public SharpTreeNode Node;
public int Index;
}
DropTarget GetDropTarget(SharpTreeViewItem item, DragEventArgs e)
{
var dropTargets = BuildDropTargets(item, e);
var y = e.GetPosition(item).Y;
foreach (var target in dropTargets) {
if (target.Y >= y) {
return target;
}
}
return null;
}
List<DropTarget> BuildDropTargets(SharpTreeViewItem item, DragEventArgs e)
{
var result = new List<DropTarget>();
var node = item.Node;
if (AllowDropOrder) {
TryAddDropTarget(result, item, DropPlace.Before, e);
}
TryAddDropTarget(result, item, DropPlace.Inside, e);
if (AllowDropOrder) {
if (node != null && node.IsExpanded && node.Children.Count > 0) {
var firstChildItem = ItemContainerGenerator.ContainerFromItem(node.Children[0]) as SharpTreeViewItem;
TryAddDropTarget(result, firstChildItem, DropPlace.Before, e);
}
else {
TryAddDropTarget(result, item, DropPlace.After, e);
}
}
var h = item.ActualHeight;
var y1 = 0.2 * h;
var y2 = h / 2;
var y3 = h - y1;
if (result.Count == 2) {
if (result[0].Place == DropPlace.Inside &&
result[1].Place != DropPlace.Inside) {
result[0].Y = y3;
}
else if (result[0].Place != DropPlace.Inside &&
result[1].Place == DropPlace.Inside) {
result[0].Y = y1;
}
else {
result[0].Y = y2;
}
}
else if (result.Count == 3) {
result[0].Y = y1;
result[1].Y = y3;
}
if (result.Count > 0) {
result[result.Count - 1].Y = h;
}
return result;
}
void TryAddDropTarget(List<DropTarget> targets, SharpTreeViewItem item, DropPlace place, DragEventArgs e)
{
SharpTreeNode node;
int index;
GetNodeAndIndex(item, place, out node, out index);
if (node != null) {
e.Effects = DragDropEffects.None;
if (node.CanDrop(e, index)) {
DropTarget target = new DropTarget() {
Item = item,
Place = place,
Node = node,
Index = index
};
targets.Add(target);
}
}
}
void GetNodeAndIndex(SharpTreeViewItem item, DropPlace place, out SharpTreeNode node, out int index)
{
node = null;
index = 0;
if (item.Node == null) {
}
else if (place == DropPlace.Inside) {
node = item.Node;
index = node.Children.Count;
}
else if (place == DropPlace.Before) {
if (item.Node.Parent != null) {
node = item.Node.Parent;
index = node.Children.IndexOf(item.Node);
}
}
else {
if (item.Node.Parent != null) {
node = item.Node.Parent;
index = node.Children.IndexOf(item.Node) + 1;
}
}
}
SharpTreeNodeView previewNodeView;
InsertMarker insertMarker;
DropPlace previewPlace;
enum DropPlace
{
Before, Inside, After
}
public Func<Brush> GetPreviewInsideTextBackground = () => SystemColors.HighlightBrush;
public Func<Brush> GetPreviewInsideForeground = () => SystemColors.HighlightTextBrush;
void ShowPreview(SharpTreeViewItem item, DropPlace place)
{
previewNodeView = item.NodeView;
previewPlace = place;
if (place == DropPlace.Inside) {
previewNodeView.TextBackground = GetPreviewInsideTextBackground();
previewNodeView.Foreground = GetPreviewInsideForeground();
}
else {
if (insertMarker == null) {
var adornerLayer = AdornerLayer.GetAdornerLayer(this);
var adorner = new GeneralAdorner(this);
insertMarker = new InsertMarker();
adorner.Child = insertMarker;
adornerLayer.Add(adorner);
}
insertMarker.Visibility = Visibility.Visible;
var p1 = previewNodeView.TransformToVisual(this).Transform(new Point());
var p = new Point(p1.X + previewNodeView.CalculateIndent(item.Node) + 4.5, p1.Y - 3);
if (place == DropPlace.After) {
p.Y += previewNodeView.ActualHeight;
}
insertMarker.Margin = new Thickness(p.X, p.Y, 0, 0);
SharpTreeNodeView secondNodeView = null;
var index = flattener.IndexOf(item.Node);
if (place == DropPlace.Before) {
if (index > 0) {
secondNodeView = (ItemContainerGenerator.ContainerFromIndex(index - 1) as SharpTreeViewItem).NodeView;
}
}
else if (index + 1 < flattener.Count) {
secondNodeView = (ItemContainerGenerator.ContainerFromIndex(index + 1) as SharpTreeViewItem).NodeView;
}
var w = p1.X + previewNodeView.ActualWidth - p.X;
if (secondNodeView != null) {
var p2 = secondNodeView.TransformToVisual(this).Transform(new Point());
w = Math.Max(w, p2.X + secondNodeView.ActualWidth - p.X);
}
insertMarker.Width = w + 10;
}
}
void HidePreview()
{
if (previewNodeView != null) {
previewNodeView.ClearValue(SharpTreeNodeView.TextBackgroundProperty);
previewNodeView.ClearValue(SharpTreeNodeView.ForegroundProperty);
if (insertMarker != null) {
insertMarker.Visibility = Visibility.Collapsed;
}
previewNodeView = null;
}
}
#endregion
#region Cut / Copy / Paste / Delete Commands
static void RegisterCommands()
{
// The asm editor should be the only one removing nodes
// CommandManager.RegisterClassCommandBinding(typeof(SharpTreeView),
// new CommandBinding(ApplicationCommands.Cut, HandleExecuted_Cut, HandleCanExecute_Cut));
//
// CommandManager.RegisterClassCommandBinding(typeof(SharpTreeView),
// new CommandBinding(ApplicationCommands.Copy, HandleExecuted_Copy, HandleCanExecute_Copy));
//
// CommandManager.RegisterClassCommandBinding(typeof(SharpTreeView),
// new CommandBinding(ApplicationCommands.Paste, HandleExecuted_Paste, HandleCanExecute_Paste));
//
// CommandManager.RegisterClassCommandBinding(typeof(SharpTreeView),
// new CommandBinding(ApplicationCommands.Delete, HandleExecuted_Delete, HandleCanExecute_Delete));
}
static void HandleExecuted_Cut(object sender, ExecutedRoutedEventArgs e)
{
}
static void HandleCanExecute_Cut(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}
static void HandleExecuted_Copy(object sender, ExecutedRoutedEventArgs e)
{
}
static void HandleCanExecute_Copy(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}
static void HandleExecuted_Paste(object sender, ExecutedRoutedEventArgs e)
{
}
static void HandleCanExecute_Paste(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
}
static void HandleExecuted_Delete(object sender, ExecutedRoutedEventArgs e)
{
SharpTreeView treeView = (SharpTreeView)sender;
treeView.updatesLocked = true;
int selectedIndex = -1;
try {
foreach (SharpTreeNode node in treeView.GetTopLevelSelection().ToArray()) {
if (selectedIndex == -1)
selectedIndex = treeView.flattener.IndexOf(node);
node.Delete();
}
} finally {
treeView.updatesLocked = false;
treeView.UpdateFocusedNode(null, Math.Max(0, selectedIndex - 1));
}
}
static void HandleCanExecute_Delete(object sender, CanExecuteRoutedEventArgs e)
{
SharpTreeView treeView = (SharpTreeView)sender;
e.CanExecute = treeView.GetTopLevelSelection().All(node => node.CanDelete());
}
/// <summary>
/// Gets the selected items which do not have any of their ancestors selected.
/// </summary>
public IEnumerable<SharpTreeNode> GetTopLevelSelection()
{
var selection = this.SelectedItems.OfType<SharpTreeNode>();
var selectionHash = new HashSet<SharpTreeNode>(selection);
return selection.Where(item => item.Ancestors().All(a => !selectionHash.Contains(a)));
}
#endregion
public void SetSelectedNodes(IEnumerable<SharpTreeNode> nodes)
{
this.SetSelectedItems(nodes.ToList());
}
}
}