Programmatically add to Window.Resources in WPF

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 similar to what I expect from him, for example:

 Window.Resources.MergedDictionaries.Add(myResourceDictionary); 

Thanks in advance,

+4
source share
1 answer

You can not:

 Window.Resources 

However, you can do:

 this.Resources.MergedDictionaries.Add(myResourceDictionary); 

Resources is a property of FrameworkElement and is shared by Application and Window (and most other user interface classes in WPF). However, this is an instance property, not a static property, so you need to work with the resources of a specific instance. When you entered Window.Resources, you tried to add a window to the type, not a specific window.

This works in your application line, because Application.Current returns the current instance of the application, so you are working with the correct, specific instance (and not the type).

+7
source

Source: https://habr.com/ru/post/1315993/


All Articles