为什么忽略XML DataTemplates? [英] Why are XML DataTemplates ignored?

查看:145
本文介绍了为什么忽略XML DataTemplates?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,WPF忽略了DataTemplate。

In the following sample, the DataTemplate is ignored by WPF.

为什么是这样?

<Window x:Class="TestXmlNonBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Xml="clr-namespace:System.Xml;assembly=System.Xml">
<Window.Resources>
    <DataTemplate DataType="{x:Type Xml:XmlDocument}">
        <TextBlock>Hello</TextBlock>
    </DataTemplate>
</Window.Resources>
<Window.DataContext>
    <Xml:XmlDocument></Xml:XmlDocument>
</Window.DataContext>
<Grid>
    <ContentControl Content="{Binding}"></ContentControl>
</Grid>

推荐答案

我认为问题是绑定,而不是模板选择。

I believe the issue is with binding, not template selection.

如果您查看 Binding.XPath 的文档,则会发现绑定源是XML数据(即 XmlDocument XmlNode XPath 属性被使用,而不是 Path 属性来查找数据源上的属性。

If you look at the documentation for Binding.XPath, you'll see that when the binding source is XML data (i.e. an XmlDocument or XmlNode) the XPath property is used, rather than the Path property, to find the property on the data source.

我怀疑这里发生的是 Binding 不会返回一个 XmlDocument 。绑定会看到源对象是一个 XmlDocument ,它调用了 SelectNodes ,传递给 XPath 属性作为参数。这是null(或者可能是空字符串),因此 SelectNodes 不返回任何内容。

I suspect that what's happening here is that the Binding is not returning an XmlDocument. The binding sees that the source object is an XmlDocument, and it calls SelectNodes on it, passing in the value of the XPath property as an argument. That's null (or maybe an empty string), and so SelectNodes doesn't return anything.

这篇关于为什么忽略XML DataTemplates?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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