WPF DataTemplate将XML树扁平化为列表?

时间:2009-08-12 07:31:18

标签: wpf xml xaml datatemplate

给定xml如下:

<Root>
    <Fruittree name="Apple" ID="2">
        <Branch name="West" ID="1">
            <Fruit name="Foo">
            <Fruit name="Bar">
        </Branch>
    </Fruittree>
    <!-- more fruitrees etc... -->
</Root>

将xaml与XmlDataProvider和DataTemplate一起使用,我想在列表框中显示一个列表:

Apple - West - Foo
Apple - West - Bar

因此,xml第3级中每个Fruit名称的列表中的项目。

1 个答案:

答案 0 :(得分:1)

使用绑定将当前位置设为Root / Fruittree,并使用此模板。

<DataTemplate x:Key="flattenTemplate">
    <TextBlock>
        <TextBlock.Text>
            <MultiBinding StringFormat="{} {0} - {1} - {2}">
                <Binding XPath="./Fruit/Branch/@Name" />
                <Binding XPath="./Branch/@Name" />
                <Binding XPath="./@Name" />  
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</DataTemplate>