tags:

views:

184

answers:

2

I have a WPF application which I would like to use some static resources in. I have created a Resource Library XAML file which contains a resource. I have also added a string into the Resources of the project through the Properties panel.

I assumed I could just use these resources with the binding expression:

{StaticResource ResourceName}

But visual studio is telling me the resources are not found. Do I have to include some form of reference in my XAML? The examples I have seen only include resources locally such as:

<Window.Resources>, <Page.Resources> etc

I don't want to include the resources locally because I want them to be available to multiple parts of the application.

+1  A: 

Put them in the App.xaml :)

In each WPF application there is an App.xaml (and its corresponding code file, App.xaml.cs or App.xaml.vb) which works as a global file for application wide declarations. You can use this file to declare a constant look and feel across your application. For example you can declare a default red background for all buttons in an application.

http://www.microsoft.com/emea/msdn/thepanel/en/articles/introduction_wpf.aspx

Snake
In my project this was named by default as Application.xaml. It's a good answer but can I not make the styles in my ResourceLibrary available globally?
Banford
A: 

Look at the section on "static resource lookup behavior" here. Anything in the application's resource dictionary (i.e. Application.Resources) is available globally. Whenever you look up a resource with a given key, and the key isn't used anywhere in the hierarchy of local resource dictionaries, the one in the application's dictionary will be returned.

To populate the application's resource dictionary in XAML, find the XAML file for the application (usually App.xaml) and add an Application.Resources element.

Robert Rossney
I can understand how the lookup behavior works its way up to this Application.Resources area but how would I use a resource that is set on the project. E.g. Right click project file, click properties, go to resources. I mean the resources in there. The are not included in my Application.Resources and neither is my Resource Library xaml file.
Banford
So I've used.. <Application.Resources> <ResourceDictionary Source="Assets/Resources/Styles.xaml" /> </Application.Resources>And that has solved one of the problems by allowing me to use the Styles in my external resource dictionary. Still struggling on the actual project resource though.
Banford
Sorry, I didn't notice that you were trying to use project resources. Project resources and resource dictionaries are really two different things. If you have (for instance) an image that's a project resource, you can get it into the resource dictionary via `<Image x:Key="SomeKey" Source="URI_of_resource"/>`. But not every WPF class has a `Source` property that can load it from the project resources.
Robert Rossney
So what about my string resource. Is it a bad idea to put this in the project resources. Should I stick it straight in the Resource Dictionary? Because I don't know how to do that. A Silverlight business application will use resources from .resx files. And the project resources file in wpf is also .resx so I know you can use a StaticResource from it. I just can't see how.
Banford
Oh, if you're using the VS resource editor, just make sure that you've set the resources' access modifier to Public. Then you can get them via markup in XAML by using `{x:Static local:Properties.Resources.MyString}`.
Robert Rossney