I'm working on an already existing project where I can't make the styling work the way I want.
I start from an application where:
- there is a WhistlerBlue.xaml file in a Resources folder.
- the styles inside the file use TargetType to automatically apply them
- there is a style defined for the Expander control (more on this, next)
- I don't want to mess with this file.
- the file is explicitly used in the application via merged resources in the App.xaml
Up to this point, everything works fine.
Then...
I'm building a custom control which is derived from Expander:
internal partial class TestExpander : Expander
By creating the custom control a Theme folder has been added to the project by VStudio, with the usual Generic.xaml file inside. In this file I've created the style for my custom control:
<Style TargetType="{x:Type local:TestExpander}" BasedOn="{StaticResource {x:Type Expander}}">
<Style.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibility"/>
</Style.Resources>
<Setter Property="ExpandDirection" Value="Down"/>
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Cursor" Value="Hand"/>
</Style>
Obviously in the code of the custom control I have:
static TestExpander()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TestExpander), new FrameworkPropertyMetadata(typeof(TestExpander)));
}
What I expect is for my TestExpander to inherit the Expander style defined in WhistlerBlue.xaml, add it's own customizations via its own style, and they all lived happily ever after.
It's not happening. Where am I wrong?
ResourceDictionary.MergedDictionaries
in your new Resource Dictionary (generic.xaml) so it can find it? – Chris W.