views:

33

answers:

1

I'm trying to set the current WPF Application ResourceDictionary programatically. (I have a WindForms project, so no "App.xaml" to do that for me).

Note: If anyone knows how to bind the equivalent of an Application.Resources to a ElementHost and all of its child controls hierarchy, this is the ultimate objective here.

I added to my code:

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Middlewerks;component/Resources.xaml", UriKind.RelativeOrAbsolute) });

Now it works perfectly in the application, the styling is fine (i.e.: Grids' backgrounds are red. It's just a test style).

But if I open this form in the designer, Visual Studio goes crazy. The whole window uses my style!

Here's a screenshot: http://localhostr.com/files/8368cc/Failure.jpg The cool part is that I found how to edit the Visual Studio 2010 ugly blue skin. The sad part is that won't make my customers happy when they develop with my control.

Feel free to try it and tell me how I should implement my resources without screwing everything up.

XAML Code: (shown in screenshot)

EDIT: Here is my temporary, very hackish solution so I can keep on developing. It really is a pain that "Application.Current" works on Visual Studio.

if (Application.Current.MainWindow == null || !Application.Current.MainWindow.Title.EndsWith(" - Microsoft Visual C# 2010 Express"))
{
    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Middlewerks;component/Resources.xaml", UriKind.RelativeOrAbsolute) });
}
A: 

When I worked on a WinForms project that used WPF areas, I just used MergedDictionaries to bring in the resources I needed, whenever I needed them.

<Window.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="DefaultResourceDictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Window.Resources>

Can you get away with that? You can still put a code behind on the dictionary if you need to do more programmatically.

You can use this at any level on any element. That is, it doesn't have to be a Window as shown here.

Drew Noakes
Good, now it's not replaced completely. Only, all the VS buttons are red. Quite funny, I just found out how to edit the VS theme. :) Didn't know it was written in WPF.
Lazlo
I can't really get away with that. I create default WPF controls programatically (i.e. buttons, expanders, labels) and need them to be styled throughout the whole ElementHost. In fact, what I truly need to do is to *bind a resource dictionary to an element host*. If I set the ElementHost.Child.Resources property, it only applies to the root, I need it to affect all child controls as well.
Lazlo