如何动态地将子节点添加到WPF TreeView?

时间:2019-06-16 16:38:56

标签: c# wpf treeview

我有这个学生名单。我想将数百个子节点动态添加到树视图中。我可以添加根项目,但不能添加子节点。我该怎么办?

我的代码-

        public List<MyItem> Items { get; private set; }

        public class MyItem
        {
            public string Student { get; set; }
            public string Course { get; set; }
            public string Location { get; set; }
        }

        List<MyItem> list1 = new List<MyItem>();

            list1.Add(new MyItem()
            {
                Student = "Pradip Mhatre",
                Course = "Diploma",
                Location = "Mumbai"
            });

trvStudents.ItemsSource = list1;

XAML:

            <TreeView Name="trvStudents"
                      ItemsSource="{Binding Items}">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate >
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=Student}"/>
                            <TextBlock Text="{Binding Path=Course}"/>
                            <TextBlock Text="{Binding Path=Location}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

非常感谢您的帮助。

0 个答案:

没有答案