将XmlDataProvider绑定到ListView时为单行

本文关键字:单行 ListView XmlDataProvider 绑定 | 更新日期: 2023-09-27 18:00:07

我想在ListView中显示XmlData。XmlDataProvider已正确填充,但Listview仅显示第一行。要显示XmlDocument中的所有行,我必须更改什么?

<Window.Resources>
    <XmlDataProvider x:Key="xmlData"/>
</Window.Resources>
<DockPanel>
    <ListView Padding="4">
        <ListView.ItemsSource>
            <Binding Source="{StaticResource xmlData}" XPath="*"/>
        </ListView.ItemsSource>
        <ListView.View>
            <GridView >
                <GridViewColumn Header="Info" Width="256" DisplayMemberBinding="{Binding XPath=Message}"/>
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>

在代码背后:

private void RefreshList(XmlDocument doc)
{
    XmlDataProvider provider = (XmlDataProvider)FindResource("xmlData");
    provider.Document = doc;
    provider.Refresh();
}

将XmlDataProvider绑定到ListView时为单行

好的,所以我用这种方式解决了它:

  1. 将XPath添加到XmlDataProvider
  2. 使用Path=InnerText而不是XPath

请参阅下面的来源。

<Window.Resources>
    <XmlDataProvider x:Key="xmlData" XPath="//Prototype"/>
</Window.Resources>
<DockPanel>
    <ListView>
        <ListView.ItemsSource>
            <Binding Source="{StaticResource xmlData}" XPath="Message"/>
        </ListView.ItemsSource>
        <ListView.View>
            <GridView >
                <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Path=InnerText}"/>
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>