将 XML 多重绑定到 TreeView

发布于 2024-10-19 21:49:23 字数 1929 浏览 1 评论 0原文

我有以下 xml 结构并想将其添加到树视图中。下面是我的第一次测试。 我的问题是我不知道如何将列表绑定到树视图,因为子元素必须由 poth 元素(类别和卡片)组成。

-cards
  -category
    -card
    -card
    -category
      -card
      -card
      -card

<Window.Resources>
    <HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=card}">
        <TextBlock Text="My Cards" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=card}">
        <TextBlock Text="{Binding XPath=@name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="card" ItemsSource="{Binding XPath=category}">
        <TextBlock Text="{Binding XPath=@name}"/>
    </HierarchicalDataTemplate>

    <XmlDataProvider x:Key="dataxml" XPath="cards" Source="folder\cards.xml" />
</Window.Resources>

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

XML 示例:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <settings>
    ....
    ..
  </settings>
  <cards>
    <category name="C1">
        <card name="card1">
            <question>bla</question>
            <answer>blub</answer>
        </card>
        <category name="C2">
            <card name="card4">
                <question>bla</question>
                <answer>blub</answer>
            </card>
        </category>
    </category>
        <card name="card2">
            <question>bla</question>
            <answer>blub</answer>
        </card>
        <card name="card3">
            <question>bla</question>
            <answer>blub</answer>
        </card>
  </cards>
</root>

i have the following xml structure and want to add it to a treeview. below is my first test.
my problem is that i don´t know how to bind the list to the treeview because the childelements must consists of poth elements (category & card).

-cards
  -category
    -card
    -card
    -category
      -card
      -card
      -card

<Window.Resources>
    <HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=card}">
        <TextBlock Text="My Cards" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=card}">
        <TextBlock Text="{Binding XPath=@name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="card" ItemsSource="{Binding XPath=category}">
        <TextBlock Text="{Binding XPath=@name}"/>
    </HierarchicalDataTemplate>

    <XmlDataProvider x:Key="dataxml" XPath="cards" Source="folder\cards.xml" />
</Window.Resources>

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

Sample of the XML:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <settings>
    ....
    ..
  </settings>
  <cards>
    <category name="C1">
        <card name="card1">
            <question>bla</question>
            <answer>blub</answer>
        </card>
        <category name="C2">
            <card name="card4">
                <question>bla</question>
                <answer>blub</answer>
            </card>
        </category>
    </category>
        <card name="card2">
            <question>bla</question>
            <answer>blub</answer>
        </card>
        <card name="card3">
            <question>bla</question>
            <answer>blub</answer>
        </card>
  </cards>
</root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2024-10-26 21:49:23

通用方法可以像这样完成;恕我直言,如果数据确实是任意的,那么这对于数据来说是一个很好的方法。

       <HierarchicalDataTemplate x:Key="NodeTemplate">
            <TextBlock x:Name="tb"/>
            <HierarchicalDataTemplate.ItemsSource>
                <Binding XPath="child::node()" />
            </HierarchicalDataTemplate.ItemsSource>
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                    <Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
       </HierarchicalDataTemplate>

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

TreeView 看起来像这样...

<TreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="treeView"
                  ItemTemplate="{StaticResource NodeTemplate}"
                  ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}" />

如果数据不是任意的,我建议通过 LINQ 或其他方法解析 XML,并将数据移动到复合类型中,然后可以通过以更细粒度的方式构建 HierarchicalDataTemplate。如果您要走这条路线 伟大的演练应该足以实现你所追求的目标。

A generic approach can be done like this; which IMHO is a nice approach for the data if it is indeed arbitrary.

       <HierarchicalDataTemplate x:Key="NodeTemplate">
            <TextBlock x:Name="tb"/>
            <HierarchicalDataTemplate.ItemsSource>
                <Binding XPath="child::node()" />
            </HierarchicalDataTemplate.ItemsSource>
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                    <Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
       </HierarchicalDataTemplate>

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

The TreeView would then look like this...

<TreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="treeView"
                  ItemTemplate="{StaticResource NodeTemplate}"
                  ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}" />

If the data is not arbitrary I would suggest parsing the XML via LINQ or other method and moving the data into a composite type which would could then be bound to via the HierarchicalDataTemplate in a more granular fashion. If you were to go that route a great walk through should suffice in achieving what you are after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文