Click here to Skip to main content
15,354,545 members
Articles / Desktop Programming / WPF
Posted 7 Feb 2013


12 bookmarked

Simply Using WPF Styles in Other Assemblies, or Other Project Types, like WinForms

Rate me:
Please Sign up or sign in to vote.
4.93/5 (11 votes)
9 Jun 2013CPOL1 min read
How to use WPF Styles in other assemblies, or other project types, like WinForms


Many developers have trouble with designer support of WPF styles, which are defined in another assembly, and should be referenced using DynamicResource and ComponentResourceKey. I will show you how to solve this problem by writing a very simple MarkupExtension.


The main problem of the XAML designer when working with external ResourceDictionaries, and impossibility to have App.xaml with included:

<Application x:Class="ResTest.App"
        <ResourceDictionary Source="pack://application:,,,/SF.Styles;component/General.xaml" />

It cannot load specific style assembly and cannot resolve ResourceKey.

<Button Style="{DynamicResource ResourceKey=MyButton}" />

Many developers use a well known cure for this, which includes using of ComponentResourceKey, Static class with style names and very difficult two side bindings (in Styles project and in Own project).

You can read about this implementation, for example, here.

Let's Do It...

All that we need - is to write a small portion of code.

Create base class with abstract MarkupExtension

using System;
using System.Windows;
using System.Windows.Markup;
namespace WPFExtensions
    public abstract class StyleRefExtension : MarkupExtension
        /// <summary>
        /// Property for specific resource dictionary
        /// </summary>
        protected static ResourceDictionary RD;
        /// <summary>
        /// Resource key which we want to extract
        /// </summary>
        public String ResourceKey { get; set; }
        /// <summary>
        /// Overriding base function which will return key from RD
        /// </summary>
        /// <param name="serviceProvider">Not used</param>
        /// <returns>Object from RD</returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
            if(RD == null) throw new Exception(
                @"You should define RD before usage. 
                Please make it in static constructor of extending class!");
            return RD[ResourceKey];

Create our styles assembly with any resource dictionaries and one compound dictionary

Image 1


<ResourceDictionary xmlns=""
    <Style TargetType="Button" x:Key="MyButton">        
        <Setter Property="Background" Value="Green" />
        <Setter Property="Width" Value="100" />
        <Setter Property="Height" Value="40" />
        <Setter Property="Content" Value="Hello from style" />        


        <ResourceDictionary Source="Dictionaries/Buttons.xaml" />

Create class in styles assembly which will extend abstract StyleRefExtension:

using System;
using System.Windows;
using WPFExtensions;

namespace MyStyles
    public class MyStyleRefExtension : StyleRefExtension
        static MyStyleRefExtension()
            RD = new ResourceDictionary()
                         Source = new Uri("pack://application:,,,/MyStyles;component/General.xaml")

You can use this class in any project and XAML designer will show you your styles.

For example, I will create a WinForms project and will add WPF user control, and for example will use DynamicResource with referenced assembly:

Image 2

Yes, not working as expected...

Now, we will use our new extension:

Image 3

And... that is what we wanted.


  • 07/02/2013 - Article creation


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Evgeny Bestfator
Software Developer (Senior) Saber Interactive
Russian Federation Russian Federation
My specializations:

C# (especially multithreading)
WPF (MVVM, styling)
WCF (message inspectors, configuration)
MSSQL (administartion, creation, procedures, recursive queries, bulk processing)

Comments and Discussions

QuestionSeparate assembly for Resource dictionary Pin
sunil.praktan8-Apr-14 0:38
Membersunil.praktan8-Apr-14 0:38 
AnswerRe: Separate assembly for Resource dictionary Pin
Evgeny Bestfator10-May-14 23:28
professionalEvgeny Bestfator10-May-14 23:28 
Hi, sunil.praktan, I think the simplest way is to use inheritance, and in constructor specify style... But of course it is not very convenient, so I use it only for windows with adding additional logic for MVVM pattern. In all other cases I use styling in place. Big Grin | :-D
GeneralMy vote of 5 Pin
Niyazi Yarar30-Jul-13 1:27
MemberNiyazi Yarar30-Jul-13 1:27 
GeneralRe: My vote of 5 Pin
Evgeny Bestfator25-Mar-15 1:18
professionalEvgeny Bestfator25-Mar-15 1:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.