WPF 中派生 ControlTemplate 的问题
以下 xaml 代码有效:
<Window x:Class="DerivedTemplateBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DerivedTemplateBug"
Title="Window1" Height="300" Width="300">
<Button>
<Button.Template>
<ControlTemplate>
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock>Testing!</TextBlock>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</Window>
现在,如果您定义以下数据模板:
using System.Windows.Controls;
namespace DerivedTemplateBug
{
public class DerivedTemplate : ControlTemplate
{
}
}
然后将 ControlTemplate 交换为派生类:
<Window x:Class="DerivedTemplateBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DerivedTemplateBug"
Title="Window1" Height="300" Width="300">
<Button>
<Button.Template>
<local:DerivedTemplate>
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock>Testing!</TextBlock>
</Border>
</local:DerivedTemplate>
</Button.Template>
</Button>
</Window>
您会收到以下错误:
“DerivedTemplateBug.DerivedTemplate”类型上的 ContentPropertyAttribute 无效,未找到属性“Content”。
谁能告诉我为什么会这样?
The following xaml code works:
<Window x:Class="DerivedTemplateBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DerivedTemplateBug"
Title="Window1" Height="300" Width="300">
<Button>
<Button.Template>
<ControlTemplate>
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock>Testing!</TextBlock>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</Window>
Now, if you define the following data template:
using System.Windows.Controls;
namespace DerivedTemplateBug
{
public class DerivedTemplate : ControlTemplate
{
}
}
And then swap the ControlTemplate for the derived class:
<Window x:Class="DerivedTemplateBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DerivedTemplateBug"
Title="Window1" Height="300" Width="300">
<Button>
<Button.Template>
<local:DerivedTemplate>
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock>Testing!</TextBlock>
</Border>
</local:DerivedTemplate>
</Button.Template>
</Button>
</Window>
You get the following error:
Invalid ContentPropertyAttribute on type 'DerivedTemplateBug.DerivedTemplate', property 'Content' not found.
Can anyone tell me why this is the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我尝试此操作时,出现不同的错误:
查找
FrameworkElementFactory
,看来这是在代码中创建模板的旧方法:我的问题是,为什么要继承
ControlTemplate
?我想不出这样做的用例。如果您只是尝试在代码隐藏中创建自己的模板,MSDN 建议使用以下方法:I get a different error when I try this:
Looking up
FrameworkElementFactory
, it appears this was an old way of creating templates in code:My question is, why are you inheriting from
ControlTemplate
? I can't think of a use case for doing this. If you are simply trying to create your own templates in code-behind, MSDN recommends the following approach: