-
Notifications
You must be signed in to change notification settings - Fork 809
Expand file tree
/
Copy pathGroupExpanderStateStore.cs
More file actions
46 lines (39 loc) · 1.51 KB
/
GroupExpanderStateStore.cs
File metadata and controls
46 lines (39 loc) · 1.51 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
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
namespace NETworkManager.Controls
{
public class GroupExpanderStateStore : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
/// <summary>
/// Stores the expansion state of each group by its name.
/// </summary>
private readonly Dictionary<string, bool> _states = [];
/// <summary>
/// The indexer to get or set the expansion state of a group by its name.
/// </summary>
/// <param name="groupName">Name of the group.</param>
/// <returns>True if expanded, false if collapsed.</returns>
public bool this[string groupName]
{
get
{
// Default to expanded if not set
if (!_states.TryGetValue(groupName, out var val))
_states[groupName] = val = true;
return val;
}
set
{
if (_states.TryGetValue(groupName, out var existing) && existing == value)
return;
Debug.WriteLine("GroupExpanderStateStore: Setting state of '{0}' to {1}", groupName, value);
_states[groupName] = value;
OnPropertyChanged($"Item[{groupName}]");
}
}
}
}