简体   繁体   中英

WPF Custom Control Template Not Applied

I'm sure this question or derivatives of it have been asked a bazillion times, but I couldn't find anything that helped me solve the problem, so I'm asking. Please feel free to direct me to the duplicate that I'm sure exists but I can't find. Apparently I'm not so great with keywords.

I have a Custom Control, it has it's own Resource Dictionary used only to define the control template. This dictionary is then merged into Generic.xaml.

The problem is that when this control shows up in the UI, it has nothing inside of it. I used Snoop to find this out. The control is in the UI, but it is completely empty.

Below you'll find the items that I think are responsible for the problem. Any help or advice you can offer is greatly appreciated.

The relevant parts of my folder structure are like this:

我的目录结构

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>

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>

Try:

<ResourceDictionary.MergedDictionaries>
     <ResourceDictionary Source="pack://application:,,,/WPDSpecBuilder;component/Layouts/Templates/XAML/BasicTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>

See here for more details on Pack Uri's

Try this:

  1. Set Build Action to BasicTemplate.xaml to Page.

  2. Add reference to BasicTemplate.xaml in Generic.xaml:

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

It should works.

I think this may be as simple as changing the relative path of the merged dictionary. Try adding a / to the start of the folder path:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Layouts/Templates/XAML/BasicTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM