blob: cad935969e2de0d35a4d2f68997b3577df7f4458 (
plain)
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
|
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Windows
{
internal class CategoryExpander
: Expander
{
public CategoryExpander ()
{
DataContextChanged += OnDataContextChanged;
}
public static readonly DependencyProperty IsFilteredProperty = DependencyProperty.Register (
"IsFiltered", typeof(bool), typeof(CategoryExpander), new PropertyMetadata (false, (o, args) => ((CategoryExpander)o).OnIsFilteredChanged()));
public bool IsFiltered
{
get { return (bool) GetValue (IsFilteredProperty); }
set { SetValue (IsFilteredProperty, value); }
}
public override void OnApplyTemplate ()
{
base.OnApplyTemplate ();
UpdateValue();
}
protected override void OnExpanded ()
{
base.OnExpanded ();
SetExpanded (true);
}
protected override void OnCollapsed ()
{
base.OnCollapsed ();
SetExpanded (false);
}
protected override void OnKeyDown (KeyEventArgs e)
{
if (e.Key == Key.Down) {
SetExpanded (true);
UpdateValue();
e.Handled = true;
} else if (e.Key == Key.Up) {
SetExpanded (false);
UpdateValue();
e.Handled = true;
} else if (e.Key == Key.Space) { // Expander should have this built, not sure why it's not working
SetExpanded (!IsExpanded);
UpdateValue();
e.Handled = true;
}
base.OnKeyDown (e);
}
private bool fromFilter;
private PanelViewModel panelVm;
private void UpdateValue ()
{
if (this.panelVm == null)
return;
SetCurrentValue (IsExpandedProperty, IsFiltered || this.panelVm.GetIsExpanded (Header as string));
}
private void OnIsFilteredChanged ()
{
this.fromFilter = true;
UpdateValue ();
this.fromFilter = false;
}
private void UpdateViewModel ()
{
FrameworkElement element = this;
while (element != null && !(element is ItemsControl)) {
element = VisualTreeHelper.GetParent (element) as FrameworkElement;
}
if (element == null) {
ClearValue (IsFilteredProperty);
this.panelVm = null;
return;
}
this.panelVm = element.DataContext as PanelViewModel;
if (this.panelVm == null)
throw new InvalidOperationException ("Couldn't find valid parent");
SetBinding (IsFilteredProperty, new Binding (nameof(PanelViewModel.IsFiltering)) {
Source = this.panelVm
});
}
private void SetExpanded (bool isExpanded)
{
if (this.fromFilter)
return;
this.panelVm.SetIsExpanded ((string) Header, isExpanded);
}
private void OnDataContextChanged (object sender, DependencyPropertyChangedEventArgs e)
{
UpdateViewModel();
UpdateValue();
}
}
}
|