简体   繁体   English

C#将Treeview绑定到嵌套结构列表

[英]C# Binding treeview to list of nested structure

I am trying to bind list of level 3 to treeview in wpf mvvm. 我正在尝试将级别3的列表绑定到wpf mvvm中的treeview。 I get this list by calling function in model class. 我通过在模型类中调用函数来获得此列表。 I have class structure like below. 我有下面的类结构。

namespace Projectname.Model
{
 class Gate
{
    public string Name { get; set; }
    public string Location { get; set; }
    public string IP { get; set; }
    public int Port { get; set; }
    public string GateMode { get; set; }
    public string Type { get; set; }
    public string Status { get; set; }
};

class Floor
{
    public string Name { get; set; }
    public List<Gate> Gates { get; set; }
};

 class Building
{
    public string Name { get; set; }
    public List<Floor> Floors { get; set; }
};
}

Below is what i have tried. 下面是我尝试过的。 Here i don't know how to give datatype. 在这里,我不知道如何给数据类型。

      <TreeView ItemsSource="{Binding Buildings}" Margin="10" Height="200">
                    <TreeView.Resources>
                        <HierarchicalDataTemplate ItemsSource="{Binding Buildings}" DataType="{x:Type local:Building}">
                            <TreeViewItem Header="{Binding Name}"/>
                        </HierarchicalDataTemplate>

                    </TreeView.Resources>

                </TreeView>

Properties 属性

    Window x:Class="projectname.MainWindow"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:projectname.Model"

I am able to display building names but like one below other not like tree as shown in image below. 我能够显示建筑物名称,但是像下面一样显示一个,而不像下面的图像所示的那样显示树。 在此处输入图片说明

any help is appreciated. 任何帮助表示赞赏。

You have to specify the templates in the <TreeView.Resources> and you have to speficy them not nested. 您必须在<TreeView.Resources>指定模板,并且必须特意将它们嵌套。

For example this could be look like (as an example just for Floor and Door ). 例如,这看起来像(仅作为FloorDoor的示例)。

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:Floor}" ItemsSource="{Binding Gates}">
        <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Floors}" DataType="{x:Type local:Building}">
        <TextBlock Header="{Binding Name}"/>
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type local:Gate}">
        <TextBlock Text="{Binding Name}" />
    </DataTemplate>
</TreeView.Resources>

If you have a node which can have children use a HierarchicalDataTemplate and set's DataType to the class it should show and the ItemsSource to the collection with children. 如果您有一个可以有子节点的节点,请使用HierarchicalDataTemplate并将其DataType设置为它应显示的类,并将ItemsSource设置为有子节点的集合。

Important 重要

Your Door , Floor , etc structs have to be public and no nested and I would recommend using classes instead. 您的DoorFloor等结构必须是public且没有嵌套,我建议使用类代替。

With this example data 通过此示例数据

new Floor
{
    Name = "Floor 1",
    Doors = new List<Door>
    {
        new Door { Name = "Door 1"},
        new Door {Name = "Door 2" }
    }
},
new Floor
{
    Name = "Floor 2",
    Doors = new List<Door>
    {
        new Door { Name = "Door 3"},
        new Door {Name = "Door 4" }
    }
}

it'll look like 它看起来像

例

edit namespace problem 编辑名称空间问题

You have to specify the namespace in your xaml. 您必须在xaml中指定名称空间。

For example your Floor is in the namespace MyProject.ViewModel you have to add xmlns:something="clr-namespace:MyProject.ViewModel" in your xaml and use DataType="{x:Type something:Floor}" in your template. 例如,您的Floor位于MyProject.ViewModel命名空间中,您必须在xaml中添加xmlns:something="clr-namespace:MyProject.ViewModel"并在模板中使用DataType="{x:Type something:Floor}"

edit 编辑

Change 更改

xmlns:local="clr-namespace:projectname.Model"

to

xmlns:local="clr-namespace:CentralizedControlHMI.Model"

And make Floor , Gate , etc public because the default is private . 并且将FloorGate等设为public因为默认值为private

public class Floor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM