6

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,

1 Answer 1

8

You can't do:

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, 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.

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).

Sign up to request clarification or add additional context in comments.

2 Comments

If I do this, are the references to the resources now DynamicResource instead of StaticResource? (I took the styles off the window pages themselves and put them into a Dictionary)
@TheGeekYouNeed: Yes, basically. THere's no way for them to be resolvable at compile time, so they have to be treated dynamically...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.