自定义 ContentControl 模板化后不显示内容?

发布于 2024-10-19 09:50:28 字数 962 浏览 2 评论 0原文

我创建了名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-10-26 09:50:28

可能是您的 assemblyinfo.cs 缺少一个属性来告诉它在哪里查找资源。检查您的 assemblyinfo.cs 中是否有以下代码:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries)

)]

并确保您的样式位于 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:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries)

)]

And make sure your style is in Themes\Generic.xaml (or referenced as a merged dictionary)

春夜浅 2024-10-26 09:50:28

您需要将 TargetType 添加到您的 ControlTemplate,因为 Control 没有名为 Content 的属性:

<ControlTemplate TargetType="{x:Type Primitives:DataGridInsertRowPresenter}">

You need to add a TargetType to your ControlTemplate since Control does not have a property called Content:

<ControlTemplate TargetType="{x:Type Primitives:DataGridInsertRowPresenter}">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文