【发布时间】:2009-06-12 09:51:58
【问题描述】:
如何从 DataTemplate 派生一个新类,然后用它代替 DataTemplate?
例如,在 C# 中:
public class DerivedDataTemplate : DataTemplate
{
}
然后在 XAML 中:
<local: DerivedDataTemplate DataType="{x:Type local:SomeType}">
<Grid>
... UI here ...
</Grid>
</local:DerivedDataTemplate>
当我尝试使用我的派生数据模板类时,会生成以下异常:
System.Windows.Markup.XamlParseException 未处理 Message="'Grid' 对象无法添加到 'DerivedDataTemplate'。'System.Windows.Controls.Grid' 类型的对象无法转换为 'System.Windows.FrameworkElementFactory' 类型。
有谁知道如何解决这个异常并成功使用派生自 DataTemplate 的类?
【问题讨论】:
-
出于好奇,您为什么要从 DataTemplate 继承?
-
我基本上希望能够拥有一种特殊的数据模板,我可以将额外的数据附加到并使用内置规则进行资源搜索和模板实例化。我最终采用了不同的方式 - 但从 DataTemplate 继承会更简单。
标签: c# wpf xaml datatemplate