gpt4 book ai didi

c# - 如何将分层数据数据绑定(bind)到 WPF TreeView?

转载 作者:太空狗 更新时间:2023-10-29 20:01:59 26 4
gpt4 key购买 nike

类型看起来像这样:

class Category
{
public string Name;
public string Message;

public ObservableCollection<Category> SubCategories;
}

其中有 5 个类别,其中每个类别包含 0(无)到 3 之间的子类别。

我知道如何将非分层数据绑定(bind)到 WPF TreeView,但无法弄清楚分层数据值。

最佳答案

这是一个例子......

<!-- Create a TreeView, and have it source data from
the AnimalCategories collection -->
<TreeView ItemsSource="{x:Static local:Window1.AnimalCategories}">

<!-- Specify the template that will display a node
from AnimalCategories. I.e., one each for “Amphibians”
and “Spiders” in this sample. It will get its nested
items from the "Animals" property of each item -->
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Animals}">

<!-- Display the AnimalCategory by showing it's Category string -->
<TextBlock FontWeight="Bold" Text="{Binding Path=Category}" />

<!-- Specify the nested template for the individual Animal items
that are within the AnimalCategories. E.g. “California Newt”, etc. -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>

</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

此代码来自here我想,阅读那篇文章可能对您更有帮助。

关于c# - 如何将分层数据数据绑定(bind)到 WPF TreeView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4150036/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com