|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls; |
| 4 | + |
| 5 | +namespace NETworkManager.Controls |
| 6 | +{ |
| 7 | + public class GroupExpander : Expander |
| 8 | + { |
| 9 | + public static readonly DependencyProperty StateStoreProperty = |
| 10 | + DependencyProperty.Register( |
| 11 | + "StateStore", |
| 12 | + typeof(GroupExpanderStateStore), |
| 13 | + typeof(GroupExpander), |
| 14 | + new PropertyMetadata(null, OnStateStoreChanged)); |
| 15 | + |
| 16 | + public GroupExpanderStateStore StateStore |
| 17 | + { |
| 18 | + get => (GroupExpanderStateStore)GetValue(StateStoreProperty); |
| 19 | + set => SetValue(StateStoreProperty, value); |
| 20 | + } |
| 21 | + |
| 22 | + public static readonly DependencyProperty GroupNameProperty = |
| 23 | + DependencyProperty.Register( |
| 24 | + nameof(GroupName), |
| 25 | + typeof(string), |
| 26 | + typeof(GroupExpander), |
| 27 | + new PropertyMetadata(null, OnGroupNameChanged)); |
| 28 | + |
| 29 | + public string GroupName |
| 30 | + { |
| 31 | + get => (string)GetValue(GroupNameProperty); |
| 32 | + set => SetValue(GroupNameProperty, value); |
| 33 | + } |
| 34 | + |
| 35 | + static GroupExpander() |
| 36 | + { |
| 37 | + IsExpandedProperty.OverrideMetadata( |
| 38 | + typeof(GroupExpander), |
| 39 | + new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsExpandedChanged)); |
| 40 | + } |
| 41 | + |
| 42 | + private static void OnIsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 43 | + { |
| 44 | + var expander = (GroupExpander)d; |
| 45 | + |
| 46 | + if (expander.StateStore != null && expander.GroupName != null) |
| 47 | + expander.StateStore[expander.GroupName] = (bool)e.NewValue; |
| 48 | + } |
| 49 | + |
| 50 | + private static void OnStateStoreChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 51 | + { |
| 52 | + var expander = (GroupExpander)d; |
| 53 | + |
| 54 | + if (e.OldValue is GroupExpanderStateStore oldStore) |
| 55 | + oldStore.PropertyChanged -= expander.StateStore_PropertyChanged; |
| 56 | + |
| 57 | + if (e.NewValue is GroupExpanderStateStore newStore) |
| 58 | + newStore.PropertyChanged += expander.StateStore_PropertyChanged; |
| 59 | + |
| 60 | + expander.UpdateIsExpanded(); |
| 61 | + } |
| 62 | + |
| 63 | + private void StateStore_PropertyChanged(object sender, PropertyChangedEventArgs e) |
| 64 | + { |
| 65 | + if (e.PropertyName == $"Item[{GroupName}]") |
| 66 | + UpdateIsExpanded(); |
| 67 | + } |
| 68 | + |
| 69 | + private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 70 | + { |
| 71 | + var expander = (GroupExpander)d; |
| 72 | + |
| 73 | + expander.UpdateIsExpanded(); |
| 74 | + } |
| 75 | + |
| 76 | + private void UpdateIsExpanded() |
| 77 | + { |
| 78 | + if (StateStore == null || GroupName == null) |
| 79 | + return; |
| 80 | + |
| 81 | + // Prevent recursive updates |
| 82 | + if (IsExpanded == StateStore[GroupName]) |
| 83 | + return; |
| 84 | + |
| 85 | + IsExpanded = StateStore[GroupName]; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments