使用XmlDataProvider时,列表框内容未使用xml中的内容填充 [英] Listbox content not being populated with content from xml when using XmlDataProvider

查看:162
本文介绍了使用XmlDataProvider时,列表框内容未使用xml中的内容填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序资源指定为

I have a very simple application resource specified as

 <Application.Resources>
        <XmlDataProvider x:Key="MoreColors" XPath="/colors">
            <x:XData>
                <colors>
                    <color name="pink"/>
                    <color name="white"/>
                    <color name="black"/>
                    <color name="cyan"/>
                    <color name="gray"/>
                    <color name="magenta"/>
                </colors>
            </x:XData>
        </XmlDataProvider>
    </Application.Resources>

,然后是一个试图用xml中的元素填充listview的窗口

and then a window trying to populate a listview with elements from the xml as

<ListBox x:Name="lbColor" 
                 IsSynchronizedWithCurrentItem="True"
                 Width="248" Height="56"
                 ItemsSource="{Binding Source={StaticResource MoreColors}, XPath=color/@name}">

        </ListBox> 

但是在运行时,应用程序根本不会填充Listview.我不敢相信我无法获得像这样简单的东西来工作....

But when run the application does not populate the Listview at all. I cannot believe I havent been able to get something as simple as this to work....

推荐答案

尝试在应用程序资源中使用<colors xmlns="">代替<colors>

try using <colors xmlns="">instead of<colors> in applicaiton resource

http://msdn.microsoft.com/zh-CN/library/system.windows.data.xmldataprovider.aspx 说 "XML数据的根节点具有xmlns属性,该属性将XML名称空间设置为空字符串.这是将XPath查询应用于XAML页内联的数据岛的要求.在这种内联情况下,XAML,因此,数据岛继承了System.Windows命名空间.因此,您需要将命名空间设置为空白,以防止XPath查询受到System.Windows命名空间的限制,这会误导查询."

http://msdn.microsoft.com/en-us/library/system.windows.data.xmldataprovider.aspx says "The root node of the XML data has an xmlns attribute that sets the XML namespace to an empty string. This is a requirement for applying XPath queries to a data island that is inline within the XAML page. In this inline case, the XAML, and thus the data island, inherits the System.Windows namespace. Because of this, you need to set the namespace blank to keep XPath queries from being qualified by the System.Windows namespace, which would misdirect the queries."

这篇关于使用XmlDataProvider时,列表框内容未使用xml中的内容填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆