0

I'm relatively new to WPF. I have two dictionaries with style definitions which I later use in XAML views.

The base style (in CommonStyles.xaml):

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyProject.Theme">
    <...>
    <Style x:Key="RoundCornerButton" TargetType="{x:Type Button}">
        <...>
    </Style>
</ResourceDictionary>

The specific style (what I've have tried) (in SpecificStyles.xaml):

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyProject.Theme">
    <...>
    <Style x:Key="RoundCornerButtonWithCheck" TargetType="{x:Type Button}" BasedOn="{StaticResource RoundCornerButton}">
        <...>
    </style>
</ResourceDictionary>

What I want to do is to use the style RoundCornerButton defined within CommonStyles.xaml as the "parent" style of RoundCornerButtonWithCheck in SpecificStyles.xaml, I mean, as the value of BasedOn property (kind of inheritance).

I've also tried setting the property BasedOn this way: BasedOn="{StaticResource {local:Style RoundCornerButton}}"

As CommonStyles.xaml is inside the folder Theme/ inside MyProject/, I thought I could access it through local: namespace in some way, but I'm not sure how to do this.

I've been looking at the documentation of BasedOn property as well as other resources, but I'm still confused.

chick3n0x07CC
  • 678
  • 2
  • 10
  • 30

1 Answers1

1

If you want to be able to reference a resource defined in CommonStyles.xaml in SpecificStyles.xaml using the StaticResource markup extension, you should merge the former ResourceDictionary into the latter:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="CommonStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="RoundCornerButtonWithCheck" TargetType="{x:Type Button}" BasedOn="{StaticResource RoundCornerButton}">
        ...
    </Style>


</ResourceDictionary>
mm8
  • 163,881
  • 10
  • 57
  • 88