WPF TreeView Binding

I have a class with parent and child properties.

ADO.NET Entity Framework Hierarchical Page Class http://img148.imageshack.us/img148/6802/edmxxe8.gif

I want to display this hierarchy in a WPF tree.

Here's my XAML ...

<TreeView Margin="12" Name="TreeViewPages" ItemsSource="{Binding}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type Page}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Path=ShortTitle}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

      

And my VB code ...

Dim db As New PageEntities
Dim t = From p In db.Page.Include ("Children") _
        Where p.Parent Is Nothing _
        Select p
TreeViewPages.ItemsSource = t

But I am getting a tree with one non-templated element:

PageManager.Page

What do I need to do to make this work?

0


source to share


1 answer


You need to define the xmlns mapping for your page class in the XAML file and use it in the type declaration. The reason you are not getting a compile error is because the page class is already defined in WPF.

So, if your page class was defined in the Zack namespace, in the TreeViewBinding assembly, you need to add the following declaration to the parent container (most likely Window):

xmlns:local="clr-namespace:Zack;assembly=TreeViewBinding"

      



Then change your DataTemplate like this:

<HierarchicalDataTemplate DataType="{x:Type local:Page}" ItemsSource="{Binding Children}">

      

I'll explain my post for your original question.

+1


source







All Articles