我想将我早期的表单应用程序"翻译"为尝试遵循MVVM模式的WPF解决方案。
现在我面临的问题是我想建立一个"上下文敏感"的上下文菜单 - 我不知道如何开始。
这是来自表单应用程序的代码,用于生成层次结构和上下文菜单:
_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 中构建类似的东西?(当然有 - 谁能给我一个提示?
我相信(几乎?)在WinForms中可以做的所有事情都可以使用WPF。请查看一些与 Treeview 和 HierarchicalDataTemplate 相关的教程。这里有几个有用的 - 这里和这里