Bind a WPF TreeView to a composite structure (composite pattern) RRS feed

  • Question


    Hi everyone,

    I am stuck in data binding a WPF TreeView to a composite structure (composite pattern).

    The composite structure in my case looks something like this:


    Project (root)

    ---WorkItem 1 (leaf)

    ---WorkItem 2 (leaf)

    ---Package 1

    ------Workitem 3 (leaf)

    ------WorkItem 4 (leaf)

    ---Workitem 5


    The TreeView should be data bound to an project object.


    Has anyone managed this?




    Friday, March 28, 2008 2:06 PM


All replies

  • Here is an example:

    Code Snippet

    <Page xmlns=""
            <XmlDataProvider x:Key="tree" XPath="*">
                    <Project  xmlns="">
            <HierarchicalDataTemplate ItemsSource="{Binding XPath=child::*}" x:Key="template">
                <TextBlock Name="textBlock" Text="{Binding Name}"/>
            ItemsSource="{Binding Source={StaticResource tree}}"
            ItemTemplate="{StaticResource template}">
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="True"/>

    Hope this helps
    Monday, March 31, 2008 6:18 AM
  • Hi Marco Zhou,

    thanks a lot for your reply. I've found a solution by using the excelent enhanced tree created by Phillip in CodeProject:


    Wednesday, April 2, 2008 2:19 PM