WPF provides a way for a Style to inherit from another Style, using the BasedOn attribute. This is basically single inheritance for styles.
We recently came across a “need” to inherit from multiple styles. Our UX designers had globally restyled the look of some of our controls, and our developers had used styles and triggers for enabling & disabling (amoung other things) some local controls.
We could have created a brand new Style — basing it on the UX Style (for example) and then cutting and pasting the style elements from the second style. However, we weren’t too keen on duplicating the second Style’s trigger logic.
Since WPF/XAML doesn’t include a built-in way to inherit from multiple styles (as it does for inheriting from a single style), we decided to create our own way of doing just that, using a custom Markup Extension.
The MergedStylesExtension merges two styles together, and returns a new Style. It does this by utilizing the BasedOn attribute to inherit from the first Style, and then it programmatically copies all of the setter and trigger elements from the second Style.
A basic version of it is as follows:
[MarkupExtensionReturnType(typeof(Style))]
public class MergedStylesExtension : MarkupExtension
{
public Style BasedOn { get; set; }
public Style MergeStyle { get; set; }
public override object ProvideValue(IServiceProvider
serviceProvider)
{
if (null == MergeStyle)
return BasedOn;
Style newStyle = new Style(BasedOn.TargetType,
BasedOn);
MergeWithStyle(newStyle, MergeStyle);
return newStyle;
} private static void MergeWithStyle(Style style, Style mergeStyle) { // Recursively merge with any Styles this Style // might be BasedOn. if (mergeStyle.BasedOn != null) { MergeWithStyle(style, mergeStyle.BasedOn); } // Merge the Setters... foreach (var setter in mergeStyle.Setters) style.Setters.Add(setter); // Merge the Triggers... foreach (var trigger in mergeStyle.Triggers) style.Triggers.Add(trigger); }
}
Using the custom extension method in XAML should be similar to using other, built-in extensions:
<Button
Style="{ext:MergedStyles
BasedOn={StaticResource FirstStyle}
MergeStyle={StaticResource SecondStyle}}"
Content="This is an example of a merged style" />
However, due to a WPF bug (see: http://www.hardcodet.net/2008/04/nested-markup-extension-bug), this won’t quite work. Instead, we need to resort to property element syntax:
<Button
Content="This is an example of a merged style">
<Button.Style>
<ext:MergedStyles
BasedOn="{StaticResource FirstStyle}"
MergeStyle="{StaticResource SecondStyle}"/>
</Button.Style>
</Button>
You can also define the merged style in the Resources section of your XAML file, if you intend to use it in more than one place:
<Style x:Key="MergedStyle" TargetType="{x:Type Control}"> <Style.BasedOn> <ext:MergedStyles BasedOn="{StaticResource FirstStyle}" MergeStyle="{StaticResource SecondStyle}"/> </Style.BasedOn> </Style>
For more information on creating your own custom Markup Extensions, see: http://dotnet.dzone.com/news/extend-wpf-add-your-own-keywor.
February 27, 2009 at 1:15 pm
Question, this helped me with an issue I’m trying solve, but only to a point. I need to support the following:
I have three base styles that may be swapped out by a choice the user makes; let’s call them Style FontSizeSmall, Style FontSizeDefault, and Style FontSizeLarger.
From there, I have a TextStyleBase style that I want to be my base style for all TextBlocks, TextBoxes, etc. (but allow another level of inheritence). It uses a class that inherits from MarkupExtension. All this class does is read from an Application property that tells which of the above styles to use. The resulting style markup looks like:
All of the the above is in the App.xaml.
In the window utilizing the style, I have this in the resources:
Then that style is applied to a TextBlock:
It works. Once. If I change the setting that the BaseStyleExtension class reads from, it doesn’t matter since it never gets called again.
I understand that what is happening is that once the style is applied, it never takes a second look at the style.
How can I make it? Any ideas?
February 27, 2009 at 1:15 pm
Crud – none of the code I pasted showed up.
October 19, 2009 at 9:43 pm
[...] Others have written great solutions to merge two styles using markup extensions. However, I wanted a solution that provided the ability to merge an unlimited number of styles, which is a little bit trickier. [...]