我正在尝试创建自己的控件来查看XmlDocuments。
我对WPF的理解是,我可以创建一个DataTemplate,它会隐式地映射到某个类型。
然后,如果我将此类型的对象分配给ContentPresenter,它将自动选择适当的DataTemplate。
那么为什么这段15行的XAML代码没有起作用呢?
<Window x:Class="TestDataTemplates.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:systemXml="clr-namespace:System.Xml;assembly=System.Xml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type systemXml:XmlDocument}">
<TextBlock Text="Look! An xml document!" />
</DataTemplate>
<systemXml:XmlDocument x:Key="TokenXmlDocument" />
</Window.Resources>
<Grid>
<ContentPresenter Content="{StaticResource ResourceKey=TokenXmlDocument}" />
</Grid>
</Window>
ContentPresenter的内容是XmlDocument,因此应该使用我为其创建的数据模板。我应该看到文本“Look! An xml document!”。
但是我看到一个空窗口。
稍后编辑:有人在这里遇到了类似的问题:为什么忽略XML数据模板?