使用 TreeView 在 XAML 中显示具有上下文相关上下文的层次结构
Use TreeView to display hierarchical structure with context-sensitive contexts in XAML
我想"translate"将我之前的表单应用程序转换为尝试遵循 MVVM 模式的 WPF 解决方案。
现在我遇到了一个问题,我想构建一个 "context-senstive" 上下文菜单 - 但我不知道如何开始。
这是来自表单应用程序的代码,用于生成层次结构和上下文菜单:
_tvLocation.Nodes.Clear();
foreach (Data.Location location in LocationManager.GetAll(this.DataSource)) {
TreeNode parent = new TreeNode(location.Name) {
Tag = location,
ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Standort löschen", new EventHandler(DelLocation_Click)) {
Tag = location
},
new MenuItem("Raum hinzufügen", new EventHandler(AddRoom_Click)) {
Tag = location
}
})
};
foreach (Data.Room room in location.GetChildren(DataSource)) {
TreeNode child = new TreeNode(room.Name) {
Tag = room,
ContextMenu = new ContextMenu(new MenuItem[]{
new MenuItem("Raum löschen", new EventHandler(DelRoom_Click)){
Tag = room
},
new MenuItem("Schrank hinzufügen", new EventHandler(AddLocker_Click)){
Tag = room
}
})
};
foreach (Data.Locker locker in room.GetChildren(this.DataSource)) {
TreeNode gradChild = new TreeNode(locker.Name) {
Tag = locker,
ContextMenu = new ContextMenu(new MenuItem[]{
new MenuItem("Schrank löschen", new EventHandler(DelLocker_Click)){
Tag = locker
}
})
};
child.Nodes.Add(gradChild);
}
parent.Nodes.Add(child);
}
_tvLocation.Nodes.Add(parent);
}
_tvLocation.ExpandAll();
有没有办法在 WPF 中构建类似的东西? (当然有 - 谁能给我提示?)
我想"translate"将我之前的表单应用程序转换为尝试遵循 MVVM 模式的 WPF 解决方案。
现在我遇到了一个问题,我想构建一个 "context-senstive" 上下文菜单 - 但我不知道如何开始。
这是来自表单应用程序的代码,用于生成层次结构和上下文菜单:
_tvLocation.Nodes.Clear();
foreach (Data.Location location in LocationManager.GetAll(this.DataSource)) {
TreeNode parent = new TreeNode(location.Name) {
Tag = location,
ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Standort löschen", new EventHandler(DelLocation_Click)) {
Tag = location
},
new MenuItem("Raum hinzufügen", new EventHandler(AddRoom_Click)) {
Tag = location
}
})
};
foreach (Data.Room room in location.GetChildren(DataSource)) {
TreeNode child = new TreeNode(room.Name) {
Tag = room,
ContextMenu = new ContextMenu(new MenuItem[]{
new MenuItem("Raum löschen", new EventHandler(DelRoom_Click)){
Tag = room
},
new MenuItem("Schrank hinzufügen", new EventHandler(AddLocker_Click)){
Tag = room
}
})
};
foreach (Data.Locker locker in room.GetChildren(this.DataSource)) {
TreeNode gradChild = new TreeNode(locker.Name) {
Tag = locker,
ContextMenu = new ContextMenu(new MenuItem[]{
new MenuItem("Schrank löschen", new EventHandler(DelLocker_Click)){
Tag = locker
}
})
};
child.Nodes.Add(gradChild);
}
parent.Nodes.Add(child);
}
_tvLocation.Nodes.Add(parent);
}
_tvLocation.ExpandAll();
有没有办法在 WPF 中构建类似的东西? (当然有 - 谁能给我提示?)