3

我正在尝试创建(我的第一个)自定义控件。为了简单起见,它包含一些不在此处提供的代码中的 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>

但是,它不适用。它编译得很好,但没有应用样式。有任何想法吗?

4

1 回答 1

4

当样式本身起作用时(正如您在评论中提到的),问题在于您的 dll 和您想要显示控件的项目之间的关系。将此添加到您AssemblyInfo.cs的 dll 项目中(它需要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)
)]
于 2013-06-02T10:11:56.827 回答