开发者

TreeView without RootNode in WPF

开发者 https://www.devze.com 2023-02-18 05:42 出处:网络
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?

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开发者_如何学JAVA=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


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消