jwillmer
jwillmer

Reputation: 3790

TreeView without RootNode in WPF

I have a HierarchicalDataTemplate that is the ItemSource of my TreeView. When the TreeView displays the data he has a rootnode. How can i remove the rootnode?

HierarchicalDataTemplate:

<Window.Resources>

    <HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=child::node()}">
        <TextBlock Text="Root"/>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=child::node()}">
        <TextBlock Text="{Binding XPath=@name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="card">
        <TextBlock Text="{Binding XPath=./title}" />
    </HierarchicalDataTemplate>

    <XmlDataProvider x:Key="dataxml" XPath="root/cards" />

</Window.Resources>

TreeView:

<TreeView Name="treeViewCategory" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}"/>

XML:

<root>
  <cards>
    <category name="Categoryname">
      <card>
        <title>something</title>
        ...
        .
      </card>
      <category name="SubCategory">
        <card>
          <title>something else</title>
          ..
          ...
        </card>
      </category>
    </category>
    <card>
      <title>text</title>
      ...
      ..
    </card>
  </cards>
</root>

actual view:

o Root
    o Categoryname
        - something
        o SubCategory
            - something else
    - text

as it should be:

o Categoryname
    - something
    o SubCategory
        - something else
- text

Upvotes: 0

Views: 2255

Answers (2)

jwillmer
jwillmer

Reputation: 3790

thx H.B. works perfect with:

<TreeView Name="treeViewCategory" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=./*}" />

i have always tried to change the source of the XmlDataProvider. haven´t imagine that i should change the Path at the TreeView :-(

why does it matter?

Upvotes: 0

brunnerh
brunnerh

Reputation: 184607

Just go one step deeper when you assign the ItemsSource of the TreeView itself so that the children of your root become the items for the tree view.

Upvotes: 4

Related Questions