首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ListBox绑定到XmlDataProvider

将ListBox绑定到XmlDataProvider
EN

Stack Overflow用户
提问于 2011-12-02 22:36:34
回答 1查看 3.2K关注 0票数 1

有人能说出为什么这不管用吗。它确实很简单,但是启动时ListBox是空的。后面的代码只包含InitializeComponent(),没有其他内容。

希望有人有主意..。

代码语言:javascript
复制
<Window x:Class="DasDataGrid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="700">

    <Window.Resources>
        <XmlDataProvider x:Key="Maschinen" XPath="/machines">
            <x:XData>
                <machines>
                    <machine name="alte Maschine"/>
                    <machine name="neue Maschine"/>
                </machines>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <ListBox ItemsSource="{Binding Source={StaticResource Maschinen},XPath=machine/@name}"
              IsSynchronizedWithCurrentItem="True"
              SelectedIndex="1">
    </ListBox>
</Window>

@H.B.这是我测试的代码。启动它时,ListBox列表仍然是空的。我不知道出了什么问题。

代码语言:javascript
复制
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <StackPanel.Resources>
        <XmlDataProvider x:Key="Maschinen">
        <x:XData>
            <machines xmlns="">
                <machine name="alte Maschine"/>
                <machine name="neue Maschine"/>
            </machines>
        </x:XData>
        </XmlDataProvider>
    </StackPanel.Resources>

    <ListBox ItemsSource="{Binding Source={StaticResource Maschinen}, XPath=machine}"
      IsSynchronizedWithCurrentItem="True" DisplayMemberPath="@name"
      SelectedIndex="1">
    </ListBox>

</StackPanel>
</Window>    
EN

回答 1

Stack Overflow用户

发布于 2011-12-02 22:41:09

您需要将xmlns设置为空字符串:

代码语言:javascript
复制
<x:XData>
    <machines xmlns="">
        <machine name="alte Maschine"/>
        <machine name="neue Maschine"/>
    </machines>
</x:XData>

MSDN

XML数据的根节点具有一个xmlns属性,该属性将XML命名空间设置为空字符串。这是将XPath查询应用于XAML页面内内联的数据岛的要求。在这种内联情况下,XAML以及数据岛继承了System.Windows命名空间。因此,您需要将名称空间设置为空白,以防止XPath查询被System.Windows命名空间限定,这会误导查询。

而且您可能想要这样绑定(即使它在结果方面没有什么不同):

代码语言:javascript
复制
<ListBox ItemsSource="{Binding Source={StaticResource Maschinen}, XPath=machine}"
          IsSynchronizedWithCurrentItem="True" DisplayMemberPath="@name"
          SelectedIndex="1">
</ListBox>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8363572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档