简体   繁体   English

数据绑定到XAML中的XmlDataProvider而不显示数据

[英]Data binding to an XmlDataProvider in XAML not displaying data

I am trying to make a very simple ListBox view that shows items from a short list as labels. 我试图做一个非常简单的ListBox视图,该视图将短列表中的项目显示为标签。 However, the window pops up blank. 但是,窗口弹出空白。 XAML: XAML:

<Window x:Class="ReCheckList.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ReCheckList"
    mc:Ignorable="d"
    Title="MainWindow" Height="298" Width="192">
<Window.Resources>
<!----- Data Source ---------->
    <XmlDataProvider x:Key="PackingListDataSource" XPath="cList">
        <x:XData>
            <Checklist xmlns="">
                <ListNode Title="Socks"/>
                <ListNode Title="Shoes"/>
                <ListNode Title="Toothbrush"/>
            </Checklist>
        </x:XData>
    </XmlDataProvider>
<!--- Data Template ----------->
    <DataTemplate x:Key="ListNodeTemplate">
        <Label Content="{Binding XPath=@Title}"></Label>
    </DataTemplate>
</Window.Resources>
<Grid>
<!------ ListBox ------->
    <ListBox ItemsSource="{Binding Source={StaticResource PackingListDataSource}, XPath=ListNode}"
             ItemTemplate="{StaticResource ListNodeTemplate}">
    </ListBox>
</Grid>

What is wrong with my data bindings? 我的数据绑定有什么问题?


EDIT: The most fundamental error was that the XPath of the XmlDataProvider did not match the root node of the xml data. 编辑:最根本的错误是XmlDataProviderXPath与xml数据的根节点不匹配。 Changing that to "Checklist" was the simplest fix, not requiring modifying my XPath search parameters. 将其更改为"Checklist"是最简单的解决方法,不需要修改XPath搜索参数。

ListNode seems just like the wrong XPath here, did you mean //ListNode or /Checklist/ListNode ? ListNode似乎就像这里的XPath一样,您的意思是//ListNode/Checklist/ListNode吗? Also, you set an XPath on the provider that does not appear in the data, would remove that. 另外,您在提供程序上设置了一个未出现在数据中的XPath ,这将删除它。

First, remove the XPath attribute from the XmlDataProvider 首先,从XmlDataProvider删除XPath属性

<XmlDataProvider x:Key="PackingListDataSource">
    <x:XData>
        <Checklist xmlns="">
            <ListNode Title="Socks"/>
            <ListNode Title="Shoes"/>
            <ListNode Title="Toothbrush"/>
        </Checklist>
    </x:XData>
</XmlDataProvider>

Then, fix the XPath in the ItemsSource binding so it returns all ListNodes in the XML. 然后,在ItemsSource绑定中修复XPath ,以便它返回XML中的所有ListNode。 In practice you might want to narrow it down more, but this at least gets me items in the ListBox . 实际上,您可能希望将其范围进一步缩小,但这至少可以使我在ListBox获得项目。

<ListBox 
    ItemsSource="{Binding Source={StaticResource PackingListDataSource}, XPath='//ListNode'}"
    ItemTemplate="{StaticResource ListNodeTemplate}"
    >
</ListBox>

// means "Search the whole XML tree for any element with this name". //表示“在整个XML树中搜索具有该名称的任何元素”。 Leaving that out, you're only searching the element you're looking at for anything with that name. 忽略这一点,您仅在搜索要查找的元素中查找具有该名称的任何内容。 In this case, you're looking at the root. 在这种情况下,您正在寻找根源。 You could explicitly start the path at root by prefacing the path with a single slash: /CheckList/ListNode . 您可以通过在路径的/CheckList/ListNode加一个斜杠/CheckList/ListNode来显式地从根开始该路径。

Alternatively (I'm learning as I go here), you could leave the binding XPath as you had it, and just change the XPath attribute of the XmlDataProvider to Checklist , so your binding will look in Checklist for ListNode elements. 或者,(我在这里学习),您可以将绑定的XPath保留不变,只需将XmlDataProviderXPath属性更改为Checklist ,这样您的绑定将在Checklist查找ListNode元素。 Your problem was really just that cList was wrong for that XPath attribute because you don't have anything called cList at the root of your XML. 您的问题确实只是该XPath属性的cList错误,因为在XML的根目录中没有任何名为cList东西。

<XmlDataProvider x:Key="PackingListDataSource" XPath="Checklist">
    <x:XData>
        <Checklist xmlns="" Title="Foo">
            <ListNode Title="Socks"/>
            <ListNode Title="Shoes"/>
            <ListNode Title="Toothbrush"/>
        </Checklist>
    </x:XData>
</XmlDataProvider>

...

<ListBox 
    ItemsSource="{Binding Source={StaticResource PackingListDataSource}, XPath='ListNode'}"
    ItemTemplate="{StaticResource ListNodeTemplate}"
    >
</ListBox>

Remember that XML is case sensitive, so Checklist is not CheckList . 请记住,XML区分大小写,因此Checklist不是CheckList And when I say you need to remember that, I really mean that I forgot it. 当我说您需要记住这一点时,我真的是说我忘记了。

<!-- change the XPath to "Checklist" -->
<XmlDataProvider x:Key="PackingListDataSource" XPath="cList">
    <x:XData>
        <Checklist xmlns="">
            <ListNode Title="Socks"/>
            <ListNode Title="Shoes"/>
            <ListNode Title="Toothbrush"/>
        </Checklist>
    </x:XData>
</XmlDataProvider>

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

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