views:

355

answers:

2

Most WPF styles I have seen are split up into one very long Theme.xaml file. I want to split mine up for readability so my Theme.xaml looks like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PresentationFramework.Aero;v3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml"/>
        <ResourceDictionary Source="Controls/Brushes.xaml"/>
        <ResourceDictionary Source="Controls/Buttons.xaml"/>
        ...
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

The problem is that this solution does not work. I have a default button style which is BasedOn the default Aero style for a button:

<Style x:Key="{x:Type Button}" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="FontSize" Value="14"/>
    ...
</Style>

If I place all of this in one file it works but as soon as I split it up I get StackOverflow exceptions because it thinks it is BasedOn itself. Is there a way around this? How does WPF add resources when merging resource dictionaries?

A: 

If both styles are always going to be applied, why not change the key on the lesser style (the one that is overridden later)? You would then reference the later one by that key rather than {StaticResource {x:Type Button}}. This should resolve the issue with determining the base type. It would also resolve a potential issue with styles being applied in the wrong order (e.g. if you redefine a single property multiple times, the order in which those dictionaries are applied would result in different results).

As long as the final descendant style has the correct key, the summation should be applied correctly.

Ben Von Handorf
My own custom button style is applied everywhere and I don't want to have to explicitly set the button style every time I create a button. I would simply like to tweak the existing default button style in PresentationFramework.Aero
anon
A: 

If you omit the BasedOn, what are the properties that don't have the values you expect? I've noticed you can safely omit the BasedOn when you want to override a few properties, and the default style's template will still get applied.

Abe Heidebrecht