开发者

How to inherit/extend styles from a resource dictionary?

开发者 https://www.devze.com 2023-01-13 20:42 出处:网络
I am having trouble extending one of my styles that I have defined in the Windows dictionary.Alone, it seems to apply the style to my controls as expected.However, if try to extend the style in one of

I am having trouble extending one of my styles that I have defined in the Windows dictionary. Alone, it seems to apply the style to my controls as expected. However, if try to extend the style in one of my userControls, using the basedOn property, it simply overrides the main one and all the base styles dissapear. Here's an example:

In a resource dictionary, named dict1.xaml:

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Pink"/>
</Style>  

In t开发者_如何学编程he main window.xaml:

<Window.Resources>
    <ResourceDictionary Source="dict1.xaml"/>
</Window.Resources>

In a user control called userControl1.xaml:

<UserControl.Resources>
    <Style  BasedOn="{StaticResource {x:Type Button}}" 
            TargetType="{x:Type Button}">
        <Setter Property="FontWeight" Value="Bold"/>
    </Style>
</UserControl.Resources>

The style in the user control simply overrides the one in the resource dictionary and the font is Bold. If I remove the style in the user control, the style in the dictionary kicks in and the background becomes pink. I want both.


You either need to add the dictionary to the UserControl resources, or add it to App.XAML resources.

As it is, the UserControl can't resolve that StaticResource -- it is in the scope of the UserControl, not the window, if that makes sense.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号