以编程方式添加到Window.Resources在WPF [英] Programmatically add to Window.Resources in WPF
问题描述
有没有办法在窗口级别而不是应用程序级别添加的ResourceDictionary
Is there a way to add a ResourceDictionary at the Window level instead of the Application level?
我看到的是这样的例子很多:
I see many examples for something like this:
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
然而,没有什么像什么,我想应该会存在,如:
However, nothing like what I would expect there to be, such as:
Window.Resources.MergedDictionaries.Add(myResourceDictionary);
由于提前,
Thanks in advance,
推荐答案
您不能做的:
Window.Resources
不过,您可以做的:
this.Resources.MergedDictionaries.Add(myResourceDictionary);
的资源是FrameworkElement的一个属性,是由应用程序和窗口(以及大多数其他用户界面类在WPF)共享。然而,这是一个实例属性,而不是一个静态属性,所以你需要与特定实例的资源的工作。当你输入Window.Resources你试图添加到窗口式的,而不是一个特定的窗口。
Resources is a property of FrameworkElement, and is shared by Application and Window (and most other user interface classes in WPF). However, it is an instance property, not a static property, so you need to work with the resources of a specific instance. When you typed "Window.Resources" you were trying to add to the "window" type, not to a specific Window.
这工作在你的应用程序,因为行<一个HREF =http://msdn.microsoft.com/en-us/library/system.windows.application.current.aspx> Application.Current 返回一个应用程序的当前实例,让你的工作用正确的,具体实例(未类型)。
This works in your Application line since Application.Current returns the current instance of an Application, so you're working with the correct, specific instance (not the type).
这篇关于以编程方式添加到Window.Resources在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!