我正在尝试创建(我的第一个)自定义控件。它包含一些DependencyProperties,为简单起见,这些不在此处提供的代码中。
public class StatusBlock : Label
{
static StatusBlock()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(StatusBlock),
new FrameworkPropertyMetadata(typeof(StatusBlock)));
}
}
现在我想应用自定义布局,并在Themes/Generic.xaml
中创建了以下行。显然,布局仅用于测试。
<Style TargetType="{x:Type Controls:StatusBlock}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:StatusBlock}">
<Grid Background="Red" MinHeight="100" MinWidth="100" >
<TextBlock Text="foobar"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但是,它不适用。编译很好,但风格不适用。有什么想法吗?
答案 0 :(得分:3)
当样式本身有效时(正如您在评论中提到的那样),问题出在您的dll和项目的关系中,您希望在其中显示控件。将此添加到dll项目的AssemblyInfo.cs
(它需要using System.Windows;
):
[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)
)]