-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeViewManager.cs
213 lines (174 loc) · 7.31 KB
/
TreeViewManager.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using DataViewer_1._0._0._0;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Diagnostics;
using ContextMenu = System.Windows.Controls.ContextMenu;
using MenuItem = System.Windows.Controls.MenuItem;
using TreeView = System.Windows.Controls.TreeView;
using System.Windows;
using System.Reflection;
namespace DataViewer_1._0._0._0
{
public static class TreeViewManager
{
// Definiere ein statisches Event um das Plotten von Daten in der Main anzustoßen
public static event Action<int> TriggerPlotData;
private static Dictionary<string, TreeViewItem> treeViewItems = new Dictionary<string, TreeViewItem>();
private static TreeView myTreeView;
public static void Initialize(TreeView treeView)
{
myTreeView = treeView;
}
/* public static void AddTreeViewItem(string comPortName, string header)
{
if (!treeViewItems.ContainsKey(comPortName))
{
TreeViewItem newItem = new TreeViewItem { Header = header};
myTreeView.Items.Add(newItem);
treeViewItems[comPortName] = newItem;
}
}*/
public static void AddTreeViewItem(string comPortName, string header)
{
if (!treeViewItems.ContainsKey(comPortName))
{
TreeViewItem newItem = new TreeViewItem { Header = header };
// Speichern von Informationen im Tag
newItem.Tag = new { ItemType = "ComPort", Name = comPortName };
newItem.ContextMenu = CreateItemContextMenu();
// Event-Handler hinzufügen
// newItem.MouseRightButtonDown += OnTreeViewItemMouseRightButtonClick;
//newItem.MouseLeftButtonDown += OnTreeViewItemMouseLeftButtonClick;
myTreeView.Items.Add(newItem);
treeViewItems[comPortName] = newItem;
}
}
/*public static void AddSubItem(string parentComPortName, string subItemName)
{
if (treeViewItems.TryGetValue(parentComPortName, out TreeViewItem parentItem))
{
TreeViewItem subItem = new TreeViewItem { Header = subItemName };
parentItem.Items.Add(subItem);
}
}*/
public static void AddSubItem(string parentComPortName, string subItemName)
{
if (treeViewItems.TryGetValue(parentComPortName, out TreeViewItem parentItem))
{
TreeViewItem subItem = new TreeViewItem { Header = subItemName };
subItem.Tag = new { ItemType = "SubItem", Name = subItemName };
subItem.ContextMenu = CreateSubItemContextMenu();
// Event-Handler hinzufügen
//subItem.MouseRightButtonDown += OnTreeViewSubItemMouseRightButtonClick;
//subItem.MouseLeftButtonDown += OnTreeViewItemMouseLeftButtonClick;
parentItem.Items.Add(subItem);
}
}
public static TreeViewItem FindTreeViewItem(string comPortName)
{
treeViewItems.TryGetValue(comPortName, out TreeViewItem item);
return item;
}
public static void RemoveTreeViewItem(string comPortName)
{
if (treeViewItems.TryGetValue(comPortName, out TreeViewItem item))
{
myTreeView.Items.Remove(item);
treeViewItems.Remove(comPortName);
}
}
public static void ClearTreeView()
{
myTreeView.Items.Clear();
treeViewItems.Clear();
}
private static ContextMenu CreateItemContextMenu()
{
ContextMenu contextMenu = new ContextMenu();
// Menüpunkt hinzufügen
MenuItem menuItem1 = new MenuItem { Header = "Read all" };
menuItem1.Click += (s, e) =>
{
MainWindow.serialPortManager.OpenPort();
MainWindow.serialPortManager.SendCommand("G");
};
MenuItem menuItem2 = new MenuItem { Header = "Read last" };
menuItem2.Click += (s, e) =>
{
MainWindow.serialPortManager.OpenPort();
MainWindow.serialPortManager.SendCommand("S");
};
MenuItem menuItem3 = new MenuItem { Header = "Aktion einfügen" };
menuItem3.Click += (s, e) => { /* Logik für Aktion 3 */ };
contextMenu.Items.Add(menuItem1);
contextMenu.Items.Add(menuItem2);
contextMenu.Items.Add(menuItem3);
// Weitere Menüpunkte und Logik basierend auf dem itemType hinzufügen
return contextMenu;
}
private static ContextMenu CreateSubItemContextMenu()
{
ContextMenu contextMenu = new ContextMenu();
// Menüpunkt hinzufügen
MenuItem menuItem1 = new MenuItem { Header = "Plot Data" };
menuItem1.Click += (s, e) =>
{
if (s is MenuItem menuItem)
{
// Finde das ContextMenu des MenuItems
ContextMenu _contextMenu = menuItem.Parent as ContextMenu;
if (_contextMenu != null)
{
// Finde das TreeViewItem, das das ContextMenu geöffnet hat
TreeViewItem subItem = _contextMenu.PlacementTarget as TreeViewItem;
TreeViewItem parentItem = LogicalTreeHelper.GetParent(subItem) as TreeViewItem;
if (parentItem != null)
{
// Event auslösen
TriggerPlotData?.Invoke(parentItem.Items.IndexOf(subItem));
}
}
}
};
MenuItem menuItem2 = new MenuItem { Header = "Export Data" };
menuItem2.Click += (s, e) =>
{
/* Logik für Aktion 2 */
};
MenuItem menuItem3 = new MenuItem { Header = "Aktion einfügen" };
menuItem3.Click += (s, e) => { /* Logik für Aktion 3 */ };
contextMenu.Items.Add(menuItem1);
contextMenu.Items.Add(menuItem2);
contextMenu.Items.Add(menuItem3);
// Weitere Menüpunkte und Logik basierend auf dem itemType hinzufügen
return contextMenu;
}
/*################################ EVENTS ###########################################*/
public static void OnTreeViewItemMouseLeftButtonClick(object sender, MouseButtonEventArgs e)
{
if (sender is TreeViewItem item)
{
var tag = item.Tag as dynamic;
if (tag != null)
{
switch (tag.ItemType)
{
case "ComPort":
// Logik für ComPort-Item
break;
case "SubItem":
// Logik für SubItem
break;
}
}
}
}
}
}