locked
TreeView + HierarchicalDataTemplate RRS feed

  • Question

  • I'd like to bind an Object form the following class to a TreeView

        class MyObject {

            private string name;
            private List<MyObject> childs;

            public string Name {
               get { return this.name; }
            }

            public List<MyObject> Childs {
               get { return this.childs; }
            }
        }

    My Treeview looks like:

    <UserControl>
        <UserControl.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:MyObject}
                ItemsSource="{Binding Path=Childs}">
                <TreeViewItem>
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=Name}" />
                        </StackPanel>
                    </TreeViewItem.Header>
                </TreeViewItem>
            </HierarchicalDataTemplate>
        </UserControl.Resources>
         
        <Grid x:Name="TreeViewGrid">
            <TreeView x:Name="NavigatorTree"
                ItemsSource="{Binding Path=Childs}"/>

        </Grid>
    </UserControl>

    The code-behind:
        ...
        MyObject rootNode = ...
        NavigatorTree.DataContext = rootNode;
        ...

    The tree shows the right data and in the correct order. But between the '+/-' Sign and the Header-Text is a gap. I can only select an item by clickin on the gap befor the item, not by clickin on the item's text.

    Do you have any ideas?
        -Daniel



    Thursday, March 30, 2006 8:26 AM

Answers

  •  Daniel F. wrote:
    In this example the Data comes from a XML-file. In my application, I use Objects. Do you think it works?  Can I use XPath with Object too?

    Try something like this (I haven't compiled it so please forgive me!)

    <UserControl>
        <UserControl.Resources>
           <ObjectDataProvider
              
    x:Key="myCollection"
              
    ObjectType="{x:Type local:MyObject}"
    />

            <HierarchicalDataTemplate DataType="{x:Type local:MyObject}
                ItemsSource="{Binding StaticResource myCollection}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:MyObject} 
                ItemsSource="{Binding StaticResource myCollection}">
                <TextBlock Text="{Binding Childs}" />
            </HierarchicalDataTemplate>
        </UserControl.Resources>
         
        <Grid x:Name="TreeViewGrid">
            <TreeView x:Name="NavigatorTree"
                ItemsSource="{Binding myCollection}"/>

        </Grid>
    </UserControl>

    Thursday, March 30, 2006 4:08 PM

All replies

  • Take a look at the below

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300"
        >
    <Canvas>
        <Canvas.Resources>
            <XmlDataProvider x:Key="FamilyTree" XPath="*">
        <x:XData>
                <Reporting xmlns="">
                    <Person Name="Steve">
                        <Person Name="Lisa" >
                           <Person Name="Mary" />
                            <Animal Name="Dog" >
                           <Person Name="Bill" />
                            </Animal >
                            <Animal Name="Cat" />
                            <Insect Name="Ant">
                              <Animal Name="Deer" />
                              <Animal Name="Horse" />
                            </Insect>
                            <Person Name="John" />
                        </Person>
                    </Person>
                </Reporting>
     </x:XData>
            </XmlDataProvider>
            <HierarchicalDataTemplate DataType="Person" ItemsSource ="{Binding XPath=*}">
               <TextBlock Text="{Binding XPath=@Name}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="Animal" ItemsSource ="{Binding XPath=*}">
               <TextBlock Text="{Binding XPath=@Name}" FontSize="20"/>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="Insect" >
               <TextBlock Text="{Binding XPath=@Name}" />
            </DataTemplate>
       </Canvas.Resources>
       <StackPanel>
        <TreeView ItemsSource="{Binding Source={StaticResource FamilyTree}, XPath=*}"/>
       </StackPanel>
    </Canvas>

    Which is a slightly modified (I've put the x:XData tags in) extract from Karsten Januszewski's Blog

    Cheers

    Simon

    Thursday, March 30, 2006 3:11 PM
  • I would try using one way bindings to the text block.  By default text blocks use two way bindings and are therefore editable.
    Thursday, March 30, 2006 3:21 PM
  • In this example the Data comes from a XML-file. In my application, I use Objects. Do you think it works?  Can I use XPath with Object too?
    Thursday, March 30, 2006 3:39 PM
  •  Daniel F. wrote:
    In this example the Data comes from a XML-file. In my application, I use Objects. Do you think it works?  Can I use XPath with Object too?

    Try something like this (I haven't compiled it so please forgive me!)

    <UserControl>
        <UserControl.Resources>
           <ObjectDataProvider
              
    x:Key="myCollection"
              
    ObjectType="{x:Type local:MyObject}"
    />

            <HierarchicalDataTemplate DataType="{x:Type local:MyObject}
                ItemsSource="{Binding StaticResource myCollection}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:MyObject} 
                ItemsSource="{Binding StaticResource myCollection}">
                <TextBlock Text="{Binding Childs}" />
            </HierarchicalDataTemplate>
        </UserControl.Resources>
         
        <Grid x:Name="TreeViewGrid">
            <TreeView x:Name="NavigatorTree"
                ItemsSource="{Binding myCollection}"/>

        </Grid>
    </UserControl>

    Thursday, March 30, 2006 4:08 PM