简体   繁体   English

WPF XML数据绑定仅显示一项

[英]WPF XML data binding showing only one item

I have a WPF application with a ListBox which I am trying to bind to an embedded xml file (Named "ServerList.XML", located in a folder named "Data" in my "SQLExecutor" solution. I want the XML to be embedded in the final *.exe) and I can't figure out why it doesn't work. 我有一个带ListBox的WPF应用程序,该应用程序试图绑定到我的“ SQLExecutor”解决方案中名为“ Data”的文件夹中的嵌入式xml文件(名为“ ServerList.XML”)。我希望将XML嵌入最后的* .exe),我不知道为什么它不起作用。 Or rather it only shows a single item from the XML. 或者更确切地说,它仅显示XML中的单个项目。
I've read on binding xml using xmldataprovider and tested different XPath variables but to no avail. 我已经阅读了使用xmldataprovider绑定xml的内容,并测试了不同的XPath变量,但无济于事。

This is my XML: 这是我的XML:

<?xml version="1.0" encoding="utf-8" ?>
<Servers xmlns="">
  <Server>
    <Address>10.0.100.43</Address>
    <Authentication>Windows Authentication</Authentication>
    <UserName></UserName>
    <Password></Password>
  </Server>
  <Server>
    <Address>10.0.100.45</Address>
    <Authentication>Windows Authentication</Authentication>
    <UserName></UserName>
    <Password></Password>
  </Server>
</Servers>

and my XAML 和我的XAML

    <ListBox x:Name="ListBoxServers" HorizontalAlignment="Left" Height="100" Margin="10,32,0,0" VerticalAlignment="Top" Width="134">
        <ListBox.Resources>
            <XmlDataProvider x:Key="ServerData" Source="/Data/ServerList.xml" XPath="Servers/Server"/>
        </ListBox.Resources>
        <ListBox.ItemsSource>
            <Binding Source="{StaticResource ServerData}" XPath="Address"/>
        </ListBox.ItemsSource>
    </ListBox>

I've placed the empty namespace in the XML, set the XML Build Action to Content , tried also with Resource and Embedded Resource . 我将空名称空间放置在XML中,将XML Build Action设置为Content ,还尝试了ResourceEmbedded Resource I used to have troubles with the databinding, itself, but I've hit the correct URI , so now I get a databinding but only the first item is shown from the XML: 我曾经在数据绑定本身上遇到过麻烦,但是我找到了正确的URI ,所以现在我有了一个数据绑定,但是XML中仅显示了第一项:

运行

I have tried all variations of the URI and Build Action that I know of. 我已经尝试了所有我知道的URIBuild Action变体。
Namely: 即:

  • /Data/ServerList.xml /Data/ServerList.xml
  • Data/ServerList.xml 数据/ ServerList.xml
  • ServerList.xml ServerList.xml
  • Resource/ServerList.xml 资源/ ServerList.xml
  • Resource/Data/ServerList.xml 资源/数据/ ServerList.xml
  • Resource/Other/ServerList.xml 资源/其它/ ServerList.xml
  • /Resource/ServerList.xml /Resource/ServerList.xml
  • /Resource/Data/ServerList.xml /Resource/Data/ServerList.xml
  • /Resource/Other/ServerList.xml /Resource/Other/ServerList.xml
  • pack://application:,,,/Data/ServerList.xml 包://应用:,,, /数据/ ServerList.xml
  • pack://application:,,,/Other/ServerList.xml 包://应用:,,, /其它/ ServerList.xml
  • /pack://application:,,,/Data/ServerList.xml /pack://application:,,,/Data/ServerList.xml
  • /pack://application:,,,/Other/ServerList.xml /pack://application:,,,/Other/ServerList.xml
  • /pack://application:,,,/SQLExecutor;component/ServerList.xml /pack://application:,,,/SQLExecutor;component/ServerList.xml
  • /pack://application:,,,/SQLExecutor;component/Data/ServerList.xml /pack://application:,,,/SQLExecutor;component/Data/ServerList.xml
  • /pack://application:,,,/SQLExecutor;component/Other/ServerList.xml /pack://application:,,,/SQLExecutor;component/Other/ServerList.xml
  • SQLExecutor;component/ServerList.xml SQLExecutor;组件/ ServerList.xml
  • SQLExecutor;component/Data/ServerList.xml SQLExecutor;组件/数据/ ServerList.xml
  • SQLExecutor;component/Other/ServerList.xml SQLExecutor;组分/其他/ ServerList.xml
  • /SQLExecutor;component/ServerList.xml /SQLExecutor;component/ServerList.xml
  • /SQLExecutor;component/Data/ServerList.xml /SQLExecutor;component/Data/ServerList.xml
  • /SQLExecutor;component/Other/ServerList.xml /SQLExecutor;component/Other/ServerList.xml
  • Build Action Content 建立动作内容
  • Build Action Resource 建立行动资源
  • Build Action Embedded Resouce 构建动作嵌入式资源
    I've tried all of the combinations and variations of the two above. 我已经尝试了以上两个的所有组合和变化。

You should be selecting a single element with the XmlDataProvider 's XPath . 您应该使用XmlDataProviderXPath选择单个元素。

<XmlDataProvider x:Key="ServerData" Source="/Data/ServerList.xml" XPath="/Servers" />

Then modify your Binding to select each Server elements' Address : 然后修改您的Binding以选择每个Server元素的Address

<Binding Source="{StaticResource ServerData}" XPath="Server/Address" />

This way it will select all the items, not just the first one. 这样,它将选择所有项目,而不仅仅是第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM