c# - 创建 TreeView 绑定(bind) wpf

标签 c# wpf xaml

我一直在尝试制作一个看起来像这样的 TreeView

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

我尝试过使用分层数据模板,但我仍然没有掌握我需要什么。这是我想要将 TreeView 绑定(bind)到的代码。任何有关 Xaml 的帮助将不胜感激。

我认为它看起来像

    <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;
    }
}

最佳答案

查看有关 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>

和 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; }
    }
}

ItemsSource 属性是一个 IEnumerable。

关于c# - 创建 TreeView 绑定(bind) wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12611324/

相关文章:

wpf - 如何将平移命令绑定(bind)到 WPF 中 OxyPlot 的鼠标左键?

c# - 如何仅在 XAML 中将 SelectionChanged 事件绑定(bind)到其他元素的可见性属性

c# - 是否有必要为简单的 true/false 操作创建一个转换器?

c# - 在C#中测量线程的执行时间

c# - VB.NET Strings.Chr 到 C#

c# - CSS + jQuery - 无法执行 .toggle() 并重复 jQueryTemplate Item [我必须警告你这有点让人不知所措]

wpf - 如何在 WPF 中创建透明的用户控件?

c# - WPF 自定义形状

c# - 如何通过代码生成WPF控件

c# - 转换和接口(interface)继承