开发者

Strange Visual Studio Behaviour when setting Application Resource Dictionary

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) });
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜