使用TreeView在XAML中显示具有上下文相关上下文的层次结构

时间:2015-01-22 09:11:10

标签: c# wpf xaml mvvm treeview

我想"翻译"我之前的Forms-Application到WPF解决方案试图遵循MVVM模式。

现在我正面临着我想要构建一个"背景敏感的问题" Contextmenu - 我不知道如何开始。

这是Forms-Application中用于生成层次结构和Contextmenus的代码:

_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中构建类似的东西? (当然有 - 有人可以给我一个暗示吗?)

1 个答案:

答案 0 :(得分:0)

我相信(差不多?)WPF可以在WinForms中做的一切。请查看与Treeview和HierarchicalDataTemplate相关的一些教程。以下是一对非常有用的内容 - herehere