创建树视图绑定 wpf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12611324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:34:13  来源:igfitidea点击:

Creating treeview binding wpf

c#wpfxaml

提问by GodsCrimeScene

I have been trying to make a treeview that looks something like

我一直在尝试制作一个看起来像的树视图

2001(root)
-Student1(node)
-Student2(node)

2001(根)
-Student1(节点)
-Student2(节点)

I've tried to use hierarchicaldatatemplates but I'm still not grasping what I need to. This is my code that i'm looking to bind my treeview to. Any help with the Xaml would be appriciated.

我试过使用分层数据模板,但我仍然不明白我需要什么。这是我希望将我的树视图绑定到的代码。任何有关 Xaml 的帮助都会得到帮助。

I thought it would look something like

我以为它看起来像

    <TreeView ItemsSource="{Binding CurrentClass}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Student}" ItemsSource="{Binding CurrentClass.Students}">
                    <TextBlock Text="{Binding CurrentClass.Students/FirstName}" />
                </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
public class ViewModel
{
    public FreshmenClass currentClass = new FreshmenClass();

    public ViewModel()
    {
        currentClass.Year = "2001";
        currentClass.Students.Add(new Student("Student1", "LastName1"));
        currentClass.Students.Add(new Student("Student2", "LastName2"));
    }

    public FreshmenClass CurrentClass
    {
        get { return currentClass; }
    }
}

public class FreshmenClass
{
    public string Year { get; set; }
    public List<Student> students = new List<Student>();

    public List<Student> Students
    {
        get { return students; }
        set { students = value; }
    }
}

public class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Student(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

回答by michele

Take a look to the documentation about Treeview and HierarchicalDataTemplate. Anyway I edit your example like this (XAML):

查看有关 Treeview 和 HierarchicalDataTemplate 的文档。无论如何,我像这样编辑您的示例(XAML):

<TreeView ItemsSource="{Binding CurrentClass}" >
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Students}">
                <TextBlock Text="{Binding Year}" />
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding FirstName}"> </TextBlock>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

and c#:

和 C#:

public class ViewModel 
{
    private List<FreshmenClass> currentClass;

    public ViewModel()
    {
        CurrentClass = new List<FreshmenClass>();
        FreshmenClass temp = new FreshmenClass();
        temp.Year = "2001";
        temp.Students.Add(new Student("Student1", "LastName1"));
        temp.Students.Add(new Student("Student2", "LastName2"));

        CurrentClass.Add(temp);
    }

    public List<FreshmenClass> CurrentClass
    {
        get { return currentClass; }
        set { currentClass = value; }
    }
}

the ItemsSource property is an IEnumerable.

ItemsSource 属性是一个 IEnumerable。