gpt4 book ai didi

wpf - 无法识别隐式映射到System.Xml.XmlDocument的DataTemplate

转载 作者:行者123 更新时间:2023-12-01 13:32:00 25 4
gpt4 key购买 nike

我试图创建自己的控件以查看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!一个xml文档!”。

相反,我看到一个空窗口。

后来编辑:有人在这里遇到类似的问题: Why are XML DataTemplates ignored?

最佳答案

奇怪!真。

这有效!

<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:sysxml="clr-namespace:System.Xml;assembly=System.Xml"
Title="Window2" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type sysxml:XmlDocument}">
<TextBlock Text="Look! A system Xml!" />
</DataTemplate>
<local:LocalXmlDocument x:Key="LocalXmlDocument" />
</Window.Resources>
<StackPanel x:Name="Panel">
<ContentPresenter Content="{StaticResource LocalXmlDocument}" />
</StackPanel>
</Window>

并在后面的代码中...
public class LocalXmlDocument : XmlDocument
{
}

WPF奇怪的无法解释的世界!

关于wpf - 无法识别隐式映射到System.Xml.XmlDocument的DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8351516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com