-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHierarchicalDataCommands.cs
More file actions
45 lines (37 loc) · 1.41 KB
/
Copy pathHierarchicalDataCommands.cs
File metadata and controls
45 lines (37 loc) · 1.41 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
using System.Windows.Controls;
using CSharpCodeAnalyst.Contracts;
namespace CSharpCodeAnalyst.TreeMap
{
public sealed class HierarchicalDataCommands
{
private readonly Dictionary<MenuItem, Action<IHierarchicalData>> _menuItemToAction = new Dictionary<MenuItem, Action<IHierarchicalData>>();
public bool Fill(ContextMenu menu, IHierarchicalData data)
{
if (!_menuItemToAction.Any())
{
return false;
}
foreach (var pair in _menuItemToAction)
{
var menuItem = pair.Key;
// Detach context menu items from previous shown context menu (if any)
var parent = menuItem.Parent as ContextMenu;
parent?.Items.Clear();
menuItem.IsEnabled = data is { IsLeafNode: true };
menuItem.Command = new DelegateCommand(() => OnMenuClick(menuItem, data));
menu.Items.Add(menuItem);
}
return true;
}
public void Register(string title, Action<IHierarchicalData> action)
{
var item = new MenuItem { Header = title };
_menuItemToAction[item] = action;
}
private void OnMenuClick(MenuItem item, IHierarchicalData data)
{
// Invoke subscriber with clicked data item.
_menuItemToAction[item].Invoke(data);
}
}
}