Question

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 the 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.

Any ideas what i'm doing wrong here?

thanks.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top