views:

1645

answers:

2

I have a Windows Forms application that needs to host a WPF control at runtime. I have the basic hosting and interaction complete (using an ElementHost control) and everything works fine until I try to do something that requires the WPF control to make use of some custom resource dictionaries that are defined. (The WPF control and all of it's resource dictionaries are all defined in the same WPF Control Library DLL.)

As soon as that happens, I get a bunch of errors that look like this:

System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='DocumentHeaderInterestStyle'

I have found one reference that talks about this, but it seems like the article is approaching things more from the WPF side, but I don't really want to have to make changes to the WPF control as everything works in a stand-alone WPF application.

If the only way to accomplish this is to make changes on the WPF side, I can get those changes made (I'm not responsible for the WPF control library but the person that is also works for the same company so it's not a problem other than getting his time to make the changes.) but I'm hoping for something I can do on the WinForms side to get this working.

The WPF control library has a resource dictionary file named "Default.xaml" defined in the project with the following properties:

Build Action: Page Copy to Output Directory: Do not copy Custom Tool: MSBuild:Compile

The stand-alone WPF application has the following entry in it's App.xaml file:

    <ResourceDictionary x:Uid="ResourceDictionary_1">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary x:Uid="ResourceDictionary_2" Source="/SmartClient.Infrastructure;component/Themes\Default.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

It seems like the control library should already know how to get its resources. Using the Resources.MergedDictionaries.Add() seems like it should work, but where do I get the instance of the existing dictionary?

+1  A: 

Assuming you know what resources you need (sounds like you do), you should just be able to "inject" them yourself. Something like:

var wpfControl = new ...;
wpfControl.Resources.Add(...);
elementHost.Child = wpfControl;

In your question you mention that there are existing resource dictionaries in the control library. If so, you can just do this:

var wpfControl = new ...;
wpfControl.Resources.MergedDictionaries.Add(/* instance of existing dictionary */);
elementHost.Child = wpfControl;

HTH, Kent

Kent Boogaart
Where would the "key" and "value" come from for the call to Add?
Scott Dorman
That makes more sense, but shouldn't the dictionaries already be merged? I've updated my question with some more information on how the WPF side of things looks.
Scott Dorman
A: 

For loading resource dictionaries that are embedded into the assembly, I've used the following snippet for loading them during the runtime:

//using System.Windows
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("MyResourceDictionary.xaml", UriKind.Relative);

Application.Current.Resources.MergedDictionaries.Add(dict);

This would also work for loading a dictionary in the executable directory.

Will Eddins