Raffaeu
Raffaeu

Reputation: 6973

WPF create Window programmatically throw on Resources

I have a WPF Window that need to be Unit Tested, so I need to create an instance of it using C# code. The Window is using some DataTemplate available in a separate dictionary. If I run the app everything is fine, but when I create a new instance of the Window in my unit tests, using this code:

MainWindow mockWindow = new MainWindow();

It throws the following exception:

Test method 
[xxx] threw exception: 
System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' 
Line number '48' and line position '30'. ---> 
System.Exception: Cannot find resource named 'TreeViewItemTemplate'. 
Resource names are case sensitive.

I assume that "somehow" I need to initialize the resources before creating the view. How can I do that programmatically?

Upvotes: 0

Views: 552

Answers (1)

D J
D J

Reputation: 7028

use DynamicResource instead of StaticResource.

Upvotes: 1

Related Questions