开发者

TreeView only loads top nodes

I have a node class I'm trying to use to populate a TreeView in Silverlight:

class TreeNode {
    public string Caption {get; set;};
    public IEnumerable<TreeNode> Children{get; set;}

}

My XAML is as follows:

<controls:TreeView VerticalAlignment="Stretch" 
                          x:Name="trv"
                          Height="150" >
            <controls:TreeView.ItemTemplate>
                <common:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}" >
         开发者_JS百科           <TextBlock Text="{Binding Path=Caption}" />
                </common:HierarchicalDataTemplate>
            </controls:TreeView.ItemTemplate>
        </controls:TreeView>

However, only the first tier of elements load. If I replace TextBlock with HerarchicalDataTemplate, basically hard-coding my data depth, the 2nd tier of elements are displayed. However then the 3rd tier are not.

Any ideas?


I just tried this out, you must be doing something wrong on the data binding part, since everything is working as expected. I created a simple example with your XAML below.

Output:

TreeView only loads top nodes

XAML:

<controls:TreeView VerticalAlignment="Stretch"  
                  x:Name="trv"
                  Height="150" >
    <controls:TreeView.ItemTemplate>
        <controls:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}" >
            <TextBlock Text="{Binding Path=Caption}" />
        </controls:HierarchicalDataTemplate>
    </controls:TreeView.ItemTemplate>
</controls:TreeView>

Code:

    TreeNode myNode = new TreeNode() { Caption = "Parent" };
    List<TreeNode> myChildren = new List<TreeNode>();

    myChildren.Add(new TreeNode() { Caption = "First Child" });
    myChildren.Add(new TreeNode() { Caption = "Second Child", Children = new List<TreeNode>() { new TreeNode() { Caption = "Child on 3rd Level" } } });


    myNode.Children = myChildren;
    trv.ItemsSource = new List<TreeNode>() { myNode };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜