WPF自定义控件模板未应用

Ant*_*ton 5 wpf custom-controls resourcedictionary controltemplate

我确信这个问题或它的衍生物已被问过无数次,但我找不到任何帮助我解决问题的东西,所以我问.请随时告诉我我确定存在但我找不到的副本.显然我对关键词的关注度不高.

我有一个自定义控件,它有自己的资源字典,仅用于定义控件模板.然后将该字典合并到Generic.xaml中.

问题是当这个控件出现在UI中时,它内部没有任何内容.我用Snoop找到了这个.控件位于UI中,但它完全为空.

您将在下面找到我认为对此问题负责的项目.我们非常感谢您提供的任何帮助或建议.

我的文件夹结构的相关部分是这样的:

我的目录结构

BasicTemplate.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFSpecBuilder.Layouts.Templates">

    <Style TargetType="{x:Type local:BasicTemplate}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BasicTemplate}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <TextBlock Text="This is a basic template." />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Layouts/Templates/XAML/BasicTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

Y.Y*_*hus 1

尝试这个:

  1. 将“构建操作”设置为“BasicTemplate.xaml”到“页面”。

  2. 在 Generic.xaml 中添加对 BasicTemplate.xaml 的引用:

    ResourceDictionary Source="/WPDSpecBuilder;component/Layouts/Templates/Xaml/BasicTemplate.xaml"

它应该有效。