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
|
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Xamarin.PropertyEditing.Windows
{
internal class TreeViewEx
: TreeView
{
static TreeViewEx()
{
setSelectedItem = (Action<TreeView, object>)typeof(TreeView).GetMethod ("SetSelectedItem", BindingFlags.NonPublic | BindingFlags.Instance).CreateDelegate (typeof(Action<TreeView, object>));
}
public event EventHandler ItemActivated;
protected override DependencyObject GetContainerForItemOverride ()
{
return new TreeViewItemEx();
}
protected override bool IsItemItsOwnContainerOverride (object item)
{
return item is TreeViewItemEx;
}
protected override void OnMouseDoubleClick (MouseButtonEventArgs e)
{
var inputElement = InputHitTest (e.GetPosition (this)) as UIElement;
if (inputElement != null) {
var item = inputElement.FindParentOrSelf<TreeViewItemEx>();
if (item != null && item.IsSelectable)
ItemActivated?.Invoke (this, EventArgs.Empty);
}
base.OnMouseDoubleClick (e);
}
internal TreeViewItemEx SelectedTreeItem
{
get { return this.selectedTreeItem; }
set
{
if (this.selectedTreeItem != null) {
if (this.selectedTreeItem != value)
this.selectedTreeItem.IsSelected = false;
else if (this.selectedTreeItem.IsSelected && SelectedItem == this.selectedTreeItem.DataContext)
return;
}
// This does break the selected value path features, but we don't care about those
object oldItem = this.selectedTreeItem?.DataContext;
this.selectedTreeItem = value;
if (value != null) {
value.IsSelected = true;
}
SetSelectedItem (oldItem, value?.DataContext);
}
}
private TreeViewItemEx selectedTreeItem;
private void SetSelectedItem (object oldItem, object item)
{
Dispatcher.InvokeAsync (() => {
setSelectedItem (this, item);
var e = new RoutedPropertyChangedEventArgs<object> (oldItem, item, SelectedItemChangedEvent);
RaiseEvent (e);
});
}
private static readonly Action<TreeView, object> setSelectedItem;
}
internal class TreeViewItemEx
: TreeViewItem
{
public TreeViewItemEx()
{
}
private static readonly DependencyPropertyKey IndentLevelKey = DependencyProperty.RegisterReadOnly ("IndentLevel", typeof(int), typeof(TreeViewItemEx), new PropertyMetadata());
public static readonly DependencyProperty IndentLevelProperty = IndentLevelKey.DependencyProperty;
public int IndentLevel
{
get { return (int)GetValue (IndentLevelProperty); }
private set { SetValue (IndentLevelKey, value); }
}
public static readonly DependencyProperty ChildMarginProperty = DependencyProperty.Register ("ChildMargin", typeof(Thickness), typeof(TreeViewItemEx), new PropertyMetadata());
public Thickness ChildMargin
{
get { return (Thickness)GetValue (ChildMarginProperty); }
set { SetValue (ChildMarginProperty, value); }
}
private static readonly DependencyPropertyKey IsMouseOverItemKey = DependencyProperty.RegisterReadOnly ("IsMouseOverItem", typeof(bool), typeof(TreeViewItemEx), new PropertyMetadata());
public static readonly DependencyProperty IsMouseOverItemProperty = IsMouseOverItemKey.DependencyProperty;
public bool IsMouseOverItem
{
get { return (bool)GetValue (IsMouseOverItemProperty); }
private set { SetValue (IsMouseOverItemKey, value); }
}
public static readonly DependencyProperty IsSelectableProperty = DependencyProperty.Register ("IsSelectable", typeof(bool), typeof(TreeViewItemEx), new PropertyMetadata (true));
public bool IsSelectable
{
get { return (bool)GetValue (IsSelectableProperty); }
set { SetValue (IsSelectableProperty, value); }
}
public override void OnApplyTemplate ()
{
base.OnApplyTemplate ();
int level = 0;
var parent = VisualTreeHelper.GetParent (this);
while (!(parent is TreeView)) {
parent = VisualTreeHelper.GetParent (parent);
if (parent is TreeViewItem)
level++;
}
this.topParent = parent as TreeViewEx;
IndentLevel = level;
}
protected override void OnMouseDown (MouseButtonEventArgs e)
{
if (IsSelectable) {
if (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl)) {
IsSelected = !IsSelected;
e.Handled = true;
}
}
base.OnMouseDown (e);
}
protected override void OnKeyDown (KeyEventArgs e)
{
if (e.Key == Key.Up || e.Key == Key.Down) {
var next = GetNextElement (e.Key == Key.Up) as ItemsControl;
if (!(next is TreeView)) {
if (next.HasItems) {
var tree = GetParentTree();
tree.SelectedTreeItem = null;
}
next.Focus();
e.Handled = true;
}
}
base.OnKeyDown (e);
}
protected override void OnSelected (RoutedEventArgs e)
{
TreeViewEx parent = GetParentTree();
if (!IsSelectable) {
IsSelected = false;
e.Handled = true;
parent.SelectedTreeItem = parent.SelectedTreeItem;
} else if (parent.SelectedTreeItem != this) {
parent.SelectedTreeItem = this;
}
}
protected override void OnUnselected (RoutedEventArgs e)
{
TreeViewEx parent = GetParentTree ();
if (parent.SelectedTreeItem == this) {
parent.SelectedTreeItem = null;
}
}
protected override DependencyObject GetContainerForItemOverride ()
{
return new TreeViewItemEx();
}
protected override bool IsItemItsOwnContainerOverride (object item)
{
return item is TreeViewItemEx;
}
protected override void OnMouseEnter (MouseEventArgs e)
{
base.OnMouseEnter (e);
UpdateMouseOver (e);
}
protected override void OnMouseMove (MouseEventArgs e)
{
base.OnMouseMove (e);
UpdateMouseOver (e);
}
protected override void OnMouseLeave (MouseEventArgs e)
{
base.OnMouseLeave (e);
IsMouseOverItem = false;
}
private TreeViewEx topParent;
private UIElement GetNextElement (bool up)
{
if (!up && HasItems && IsExpanded)
return (UIElement)ItemContainerGenerator.ContainerFromIndex (0);
var parentContainer = this.FindParent<ItemsControl>();
if (parentContainer == null)
return null;
int index = parentContainer.ItemContainerGenerator.IndexFromContainer (this);
index += (up) ? -1 : 1;
if (index == -1)
return parentContainer;
while (index == parentContainer.Items.Count) {
if (parentContainer is TreeView)
return parentContainer;
var parentsParent = parentContainer.FindParent<ItemsControl>();
index = parentsParent.ItemContainerGenerator.IndexFromContainer (parentContainer) + 1;
parentContainer = parentsParent;
}
var item = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromIndex (index);
if (!up || (!item.IsExpanded || !item.HasItems))
return item;
return (UIElement)item.ItemContainerGenerator.ContainerFromIndex (item.Items.Count - 1);
}
private TreeViewEx GetParentTree()
{
if (this.topParent == null)
this.topParent = this.FindParent<TreeViewEx>();
return this.topParent;
}
private void UpdateMouseOver (MouseEventArgs e)
{
UIElement element = InputHitTest (e.GetPosition (this)) as UIElement;
if (element == null) {
IsMouseOverItem = false;
return;
}
IsMouseOverItem = element.FindParentOrSelf<TreeViewItemEx>() == this;
}
}
}
|