locked
I don't understand HierarchicalDataTemplate ... RRS feed

  • Question

  • I'm writing a small sample app, and I have the following XML:

     

    <Root Value="YES">
      <Child Name="Test">
        <Sibling Data="Yes">
          <Last UserData="1"/>
        </Sibling>
        <Sibling Data="No"/>
      </Child>
      <Child Name="Test2"/>
    </Root>

     

    I want to bind this to a treeview, but I want the treeview to only show elements from the Child element and downwards, but the following HierarchicalDataTemplates don't work:

     

    <Window.Resources>
        <XmlDataProvider x:Key="dataProvider" XPath="Child" Source="C:\XML.xml" />
        
        <HierarchicalDataTemplate DataType="Child" ItemsSource="{Binding XPath=*}">
          <TextBlock Text="{Binding XPath=@Name}" />
        </HierarchicalDataTemplate>
        
        <HierarchicalDataTemplate DataType="Sibling" ItemsSource="{Binding XPath=*}">
          <TextBlock Text="{Binding XPath=@Data}" />
        </HierarchicalDataTemplate>
      </Window.Resources>

     

    What am I doing wrong ???

     

    Monday, July 5, 2010 3:08 PM

Answers

  • For XmlDataProvider.XPath, I believe we need to include the Root element. For ItemsSource, try setting the path name instead of *.

    Example:

    <Window x:Class="BindHierarchicalDataTemplateToXML.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:BindHierarchicalDataTemplateToXML"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <XmlDataProvider x:Key="myProvider" Source="XMLFile1.xml" XPath="/Root/Child"/>
            <local:MyConverter x:Key="myConverter"/>
            <HierarchicalDataTemplate DataType="Last">
                <TextBlock Text="{Binding XPath=@UserData}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="Sibling" ItemsSource="{Binding XPath=Last}">
                <TextBlock Text="{Binding XPath=@Data}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="Child" ItemsSource="{Binding XPath=Sibling}">
                <TextBlock Text="{Binding XPath=@Name}" />
            </HierarchicalDataTemplate>
        </Window.Resources>
        <StackPanel>
            <TreeView ItemsSource="{Binding Source={StaticResource myProvider}}"/>
        </StackPanel>
    </Window>


    William
    • Marked as answer by maximus37 Monday, July 5, 2010 7:26 PM
    Monday, July 5, 2010 6:35 PM

All replies

  • For XmlDataProvider.XPath, I believe we need to include the Root element. For ItemsSource, try setting the path name instead of *.

    Example:

    <Window x:Class="BindHierarchicalDataTemplateToXML.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:BindHierarchicalDataTemplateToXML"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <XmlDataProvider x:Key="myProvider" Source="XMLFile1.xml" XPath="/Root/Child"/>
            <local:MyConverter x:Key="myConverter"/>
            <HierarchicalDataTemplate DataType="Last">
                <TextBlock Text="{Binding XPath=@UserData}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="Sibling" ItemsSource="{Binding XPath=Last}">
                <TextBlock Text="{Binding XPath=@Data}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="Child" ItemsSource="{Binding XPath=Sibling}">
                <TextBlock Text="{Binding XPath=@Name}" />
            </HierarchicalDataTemplate>
        </Window.Resources>
        <StackPanel>
            <TreeView ItemsSource="{Binding Source={StaticResource myProvider}}"/>
        </StackPanel>
    </Window>


    William
    • Marked as answer by maximus37 Monday, July 5, 2010 7:26 PM
    Monday, July 5, 2010 6:35 PM
  • Thanks for the replies. William's solution worked like a charm!   :)
    Monday, July 5, 2010 7:26 PM