Treeview绑定未显示第二级

时间:2014-05-25 16:12:53

标签: wpf xaml

您好我有一个树视图,我试图深入了解2级。

第二级未显示

这是我的VM中的类和集合

public class BuildTree
{
   public int Id { get; set; } 
   public string Groups { get; set; }
   public IEnumerable<myAreas> Areas { get; set; }               
}                       
//Areas has a name property I want to show on the second level under the group

public ObservableCollection<BuildTree> MyObsvCollection
{
    get { return _myCollection; }
}

这是我的Xaml

<TreeView ItemsSource="{Binding MyObsvCollection}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate>
            <TextBlock Foreground="Red" Text="{Binding Groups}" />
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Areas}">
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

1 个答案:

答案 0 :(得分:1)

您的HierarchicalDataTemplate没有要显示的项目。您应将ItemsSource移至外HierarchicalDataTemplate,然后ItemTemplate可以简单DataTemplate

<HierarchicalDataTemplate ItemsSource="{Binding Areas}">
    <TextBlock Foreground="Red" Text="{Binding Groups}" />
    <HierarchicalDataTemplate.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>