【问题标题】:WPF XML data binding showing only one itemWPF XML 数据绑定仅显示一项
【发布时间】:2017-09-30 18:04:21
【问题描述】:

我有一个带有 ListBox 的 WPF 应用程序,我试图将它绑定到嵌入式 xml 文件(名为“ServerList.XML”,位于我的“SQLExecutor”解决方案中名为“Data”的文件夹中。我希望 XML嵌入在最终的 *.exe 中),我不知道为什么它不起作用。或者更确切地说,它只显示 XML 中的一个项目。
我已阅读 binding xml using xmldataprovider 并测试了不同的 XPath 变量,但无济于事。

这是我的 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>

还有我的 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>

我在 XML 中放置了空的命名空间,将 XML Build Action 设置为 Content,还尝试了 Resource 和 Embedded Resource。我曾经遇到过数据绑定本身的问题,但我找到了正确的URI,所以现在我得到了一个数据绑定,但只有第一项从 XML 中显示:

我已经尝试了我所知道的 URI 和 Build Action 的所有变体。
即:

  • /Data/ServerList.xml
  • 数据/ServerList.xml
  • ServerList.xml
  • 资源/ServerList.xml
  • 资源/数据/ServerList.xml
  • 资源/其他/ServerList.xml
  • /Resource/ServerList.xml
  • /Resource/Data/ServerList.xml
  • /Resource/Other/ServerList.xml
  • pack://application:,,,/Data/ServerList.xml
  • pack://application:,,,/Other/ServerList.xml
  • /pack://application:,,,/Data/ServerList.xml
  • /pack://application:,,,/Other/ServerList.xml
  • /pack://application:,,,/SQLExecutor;component/ServerList.xml
  • /pack://application:,,,/SQLExecutor;component/Data/ServerList.xml
  • /pack://application:,,,/SQLExecutor;component/Other/ServerList.xml
  • SQLExecutor;component/ServerList.xml
  • SQLExecutor;component/Data/ServerList.xml
  • SQLExecutor;component/Other/ServerList.xml
  • /SQLExecutor;component/ServerList.xml
  • /SQLExecutor;component/Data/ServerList.xml
  • /SQLExecutor;component/Other/ServerList.xml
  • 构建动作内容
  • 构建操作资源
  • 构建操作嵌入式资源
    我已经尝试了以上两者的所有组合和变体。

【问题讨论】:

  • 将 XML 文件放在名为 Data 的项目文件夹中,而不是放在解决方案文件夹中。然后Source="Data/ServerList.xml" 与构建操作Resource 一起使用。
  • @Clemens,抱歉应该提到我希望它嵌入到 *.exe 中
  • 构建操作资源就是这样。它将创建一个“装配资源”。
  • @Clemens,哦,所以你的意思是我不仅需要将它包含在解决方案中(或者我可能根本不需要将它放入解决方案中),而且还要将它物理地放在项目文件夹中的硬盘?
  • 项目文件夹是 Visual Studio 项目中的文件夹,而不是 VS 解决方案中的文件夹。但是该文件夹是强制性的,您也可以将文件放在项目根目录中,在这种情况下,它的 Uri 只是文件名。 “物理上在硬盘上”,不确定您的意思。 VS 项目中的所有文件都存储在某个地方。

标签: c# xml wpf xaml xpath


【解决方案1】:

您应该选择带有XmlDataProvider 的XPath 的单个元素。

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

然后修改您的Binding 以选择每个Server 元素的Address:

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

这样它将选择所有项目,而不仅仅是第一个。

【讨论】:

  • 您应该将Binding 的XPath 更改为Server 而不是/Server。您正在搜索位于 XML 文档根目录中并名为 Server 的项目。 XML 文档中没有类似的项目,即使您使用 XmlDataProvider 进行了预过滤(这不会为您提供新的 XML 文档)。
  • 谢谢,这有帮助,但事实证明它是区分大小写的,我错过了一个大写字母。修复后我删除了我的评论。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多