加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将分层数据绑定到WPF TreeView?

发布时间:2020-12-15 04:22:57 所属栏目:百科 来源:网络整理
导读:类型如下: class Category{ public string Name; public string Message; public ObservableCollectionCategory SubCategories;} 它会说5个类别,其中每个类别包含0(无)到3之间的子类别. 我知道如何将非分层数据绑定到WPF TreeView,但无法弄清楚分层数据值.
类型如下:
class Category
{
    public string Name;
    public string Message;

    public ObservableCollection<Category> SubCategories;
}

它会说5个类别,其中每个类别包含0(无)到3之间的子类别.

我知道如何将非分层数据绑定到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,对于你来说,阅读那篇文章可能会更有帮助,我在想.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读