BindingNavigator for WPF (Part 2–Using)

Windows Forms and WPF Interop: Sharing WPF Resources

With WPF applications where the resources should not be locally with the application but shared with multiple applications, the WPF resources can be added to a library. Using the MergedDictionaries property of a ResourceDictionary the resources from another assembly can be referenced, for global resources this is usually done within the App.xaml file. Having a Windows Forms host that makes use of WPF controls it’s also possible to use shared resources for WPF styling. This blog post shows how this can be done.

Having resources in a shared library (the sample has the library named WpfStylesLib), shared resources (Dictionary1.xaml), with a WPF application the resources can be made available application-wide by adding the resource dictionary to the MergedDictionaries in the file App.xaml. To reference the shared library I’m using the relative Pack URI syntax. Following the / is the name of the assembly: WpfStylesLib. ;component following the assembly name means the assembly is referenced from the local assembly. Dictionary1.xaml is the resource name in the referenced assembly. Now all the resources defined within this dictionary can be used from the WPF application.

<Application x:Class="WPFResourceTest.App" xmlns="" xmlns:x="" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/WpfStylesLib;component/Dictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>

With a Windows Forms host usually there’s no App.xaml. It’s easy to add the resources from a shared library programmatically. Within the Main method of Program.cs I’m creating a WPF Application instance. To not get in conflict with the Windows Forms Application class I’ve added a namespace alias named WPF to System.Windows. After a new Application instance is created, Application.Current is set. Similar to the XAML code before I’m using the Resources property of the Application, reference the MergedDictionaries, and add a ResourceDictionary to it. Here I’m using the absolute Pack URL syntax to reference the resources in the shared assembly.

WPF.Application app = new WPF.Application(); WPF.ResourceDictionary dict = new WPF.ResourceDictionary(); dict.Source = new Uri("pack://application:,,,/StylesLib;component/Dictionary1.xaml"); WPF.Application.Current.Resources.MergedDictionaries.Add(dict);

More information on WPF and Windows Forms interop in my WPF workshop


CN innovation


Feed You can follow this conversation by subscribing to the comment feed for this post.


Hi Christian,
thank you for your interesting post.
I followed your solution to add a resource dictionary to a winforms app. But I have a question: how to add it statically? I mean: if my form contains a wpf user control, resources referenced by that control using 'StaticResource'
are not resolved.

Do you know how to solve this issue? Thank you bye! Valentina

It's very simple to find out any topic on net as compared to books, as I found this paragraph at this web page.

reverse phone call

I just could not leave your website prior to suggesting that I actually loved the standard information an individual provide to your visitors? Is gonna be back ceaselessly to check out new posts

reverse search phone number

I think that everything published was actually very logical. But, what about this? what if you typed a catchier post title? I ain't suggesting your content isn't solid, but suppose you added something that grabbed people's attention? I mean Windows Forms and WPF Interop: Sharing WPF Resources - Christian Nagel's Blog is a little boring. You could peek at Yahoo's front page and see how they write article headlines to grab viewers to open the links. You might add a related video or a related picture or two to get people excited about what you've written. Just my opinion, it could make your blog a little bit more interesting.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)