C# 未应用自定义控件的样式
我正在尝试创建(我的第一个)自定义控件。为了简单起见,它包含一些不在这里提供的代码中的DependencyPropertiesC# 未应用自定义控件的样式,c#,wpf,styles,custom-controls,C#,Wpf,Styles,Custom Controls,我正在尝试创建(我的第一个)自定义控件。为了简单起见,它包含一些不在这里提供的代码中的DependencyProperties public class StatusBlock : Label { static StatusBlock() { DefaultStyleKeyProperty.OverrideMetadata(typeof(StatusBlock), new FrameworkPropertyMetadata(type
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>
然而,它并不适用。它编译得很好,但没有应用样式。有什么想法吗?当样式本身起作用时(正如您在评论中提到的),问题在于dll和您希望显示控件的项目之间的关系。将此添加到dll项目的
AssemblyInfo.cs
(它需要使用System.Windows;
):
你能发布你的自定义控件所在的xaml代码吗<代码>我无法发现您的代码有任何问题,因为我可以在我的窗口中看到样式,该窗口承载带有背景色和文本的自定义标签。我在一个程序集中有控件/样式,并在实际项目中将其作为dll使用。这会改变什么吗?我已尝试将样式放置在主项目中的Generic.xaml中,但仍然无效。只要引用Dll并创建一个xaml命名空间引用xmlns:Ctl=“clr namespace:CLabelNameSpace;assembly=CLabelAssembly”,就可以了。值得思考的是,在它使用的同一个项目中创建一个自定义控件。这将使我们有机会筛选出根本原因。
[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)
)]