[英]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
,还尝试了Resource
和Embedded 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. 我已经尝试了所有我知道的
URI
和Build Action
变体。
Namely: 即:
You should be selecting a single element with the XmlDataProvider
's XPath
. 您应该使用
XmlDataProvider
的XPath
选择单个元素。
<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.