简体   繁体   English

将WPF TreeView绑定到对象

[英]Binding WPF TreeView to Object

How to bind TreeView to the grouping collection ListCollectionView? 如何将TreeView绑定到分组集合ListCollectionView?

code cs: 代码CS:

ListCollectionView view=new ListCollectionView(Global._subjectCollection);
view.GroupDescriptions.Add(new PropertyGroupDescription("Fund"));
view.GroupDescriptions.Add(new PropertyGroupDescription("Cipher"));
treeView1.ItemsSource = view.Groups;

code XAML: 代码XAML:

<TreeView>???</TreeView>

Check out Bea Stollnitz's blog entry "How do I display grouped data in a TreeView?" 请查看Bea Stollnitz的博客条目“如何在TreeView中显示分组的数据?” .

You use a HierarchicalDataTemplate to present the CollectionViewGroup. 您使用HierarchicalDataTemplate呈现CollectionViewGroup。 The Name property will have the value of the property you grouped by, so the value of Fund or Cipher, and the Items property will have the nested group for outer groupings and the actual object for the innermost grouping. Name属性将具有您分组的属性的值,因此Fund或Cipher的值以及Items属性将具有用于外部分组的嵌套组和用于内部分组的实际对象。 It will look something like this: 它看起来像这样:

<Window.Resources>
    <!-- Template for actual object -->
    <DataTemplate x:Key="ThirdTemplate">
        <TextBlock Text="{Binding OtherData}"/>
    </DataTemplate>
    <!-- Template for Cipher groups -->
    <HierarchicalDataTemplate x:Key="SecondTemplate"
                              ItemsSource="{Binding Items}"
                              ItemTemplate="{StaticResource ThirdTemplate}">
        <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>
    <!-- Template for Fund groups -->
    <HierarchicalDataTemplate x:Key="FirstTemplate"
                              ItemsSource="{Binding Items}"
                              ItemTemplate="{StaticResource SecondTemplate}">
        <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>
</Window.Resources>
<TreeView Name="treeView1" ItemTemplate="{StaticResource FirstTemplate}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM