【发布时间】:2010-11-13 18:05:36
【问题描述】:
我确信这个问题或其衍生问题已经被问了无数次,但我找不到任何可以帮助我解决问题的东西,所以我在问。请随时将我引导到我确定存在但我找不到的副本。显然我不太擅长关键字。
我有一个自定义控件,它有自己的资源字典,仅用于定义控件模板。然后将该字典合并到 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>
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>
【问题讨论】:
-
我会通过排除原则推进:首先断言您的样式定义不是问题的一部分。因此,从嵌套的 ResourceDictionary 中挑选出来并将其粘贴到 Generic.xaml(我希望您在 App.xaml 中引用它)。之后显示所需的效果,尝试史蒂夫建议(断言您的相对路径设置正确)
标签: wpf custom-controls controltemplate resourcedictionary