-
Notifications
You must be signed in to change notification settings - Fork 809
Expand file tree
/
Copy pathGroupExpander.cs
More file actions
88 lines (71 loc) · 2.89 KB
/
GroupExpander.cs
File metadata and controls
88 lines (71 loc) · 2.89 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
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace NETworkManager.Controls
{
public class GroupExpander : Expander
{
public static readonly DependencyProperty StateStoreProperty =
DependencyProperty.Register(
"StateStore",
typeof(GroupExpanderStateStore),
typeof(GroupExpander),
new PropertyMetadata(null, OnStateStoreChanged));
public GroupExpanderStateStore StateStore
{
get => (GroupExpanderStateStore)GetValue(StateStoreProperty);
set => SetValue(StateStoreProperty, value);
}
public static readonly DependencyProperty GroupNameProperty =
DependencyProperty.Register(
nameof(GroupName),
typeof(string),
typeof(GroupExpander),
new PropertyMetadata(null, OnGroupNameChanged));
public string GroupName
{
get => (string)GetValue(GroupNameProperty);
set => SetValue(GroupNameProperty, value);
}
static GroupExpander()
{
IsExpandedProperty.OverrideMetadata(
typeof(GroupExpander),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsExpandedChanged));
}
private static void OnIsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var expander = (GroupExpander)d;
if (expander.StateStore != null && expander.GroupName != null)
expander.StateStore[expander.GroupName] = (bool)e.NewValue;
}
private static void OnStateStoreChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var expander = (GroupExpander)d;
if (e.OldValue is GroupExpanderStateStore oldStore)
oldStore.PropertyChanged -= expander.StateStore_PropertyChanged;
if (e.NewValue is GroupExpanderStateStore newStore)
newStore.PropertyChanged += expander.StateStore_PropertyChanged;
expander.UpdateIsExpanded();
}
private void StateStore_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == $"Item[{GroupName}]")
UpdateIsExpanded();
}
private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var expander = (GroupExpander)d;
expander.UpdateIsExpanded();
}
private void UpdateIsExpanded()
{
if (StateStore == null || GroupName == null)
return;
// Prevent recursive updates
if (IsExpanded == StateStore[GroupName])
return;
IsExpanded = StateStore[GroupName];
}
}
}