自定义 ContentControl 模板化后不显示内容?
我创建了名为 DataGridInsertRowPresenter
的新自定义控件,该控件继承自 ContentControl
。一切顺利。
然后我添加了新的样式来更改它的模板,并且它不再显示内容。这是控件:
public class DataGridInsertRowPresenter : ContentControl {
static DataGridInsertRowPresenter() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridInsertRowPresenter), new FrameworkPropertyMetadata(typeof(DataGridInsertRowPresenter)));
}
}
这是我的模板:
<Style TargetType="{x:Type Primitives:DataGridInsertRowPresenter}" BasedOn="{StaticResource {x:Type ContentControl}}" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的代码有什么问题?
I've created new custom control called DataGridInsertRowPresenter
that inherits from ContentControl
. Everything worked well.
Then I added new style that changes template for it and it does not display content any more. Here is the control:
public class DataGridInsertRowPresenter : ContentControl {
static DataGridInsertRowPresenter() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridInsertRowPresenter), new FrameworkPropertyMetadata(typeof(DataGridInsertRowPresenter)));
}
}
Here is my template:
<Style TargetType="{x:Type Primitives:DataGridInsertRowPresenter}" BasedOn="{StaticResource {x:Type ContentControl}}" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
What is wrong with my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是您的 assemblyinfo.cs 缺少一个属性来告诉它在哪里查找资源。检查您的 assemblyinfo.cs 中是否有以下代码:
)]
并确保您的样式位于 Themes\Generic.xaml 中(或作为合并字典引用)
It could be that your assemblyinfo.cs is missing an attribute to tell it where to look for resources. Check that you have the following code in your assemblyinfo.cs:
)]
And make sure your style is in Themes\Generic.xaml (or referenced as a merged dictionary)
您需要将
TargetType
添加到您的ControlTemplate
,因为Control
没有名为Content
的属性:You need to add a
TargetType
to yourControlTemplate
sinceControl
does not have a property calledContent
: