How can WPF resources - including styles, templates, etc. - be organized, so that I can use them across Windows, Pages or even Projects. What options do I have to achieve maximum re-usability of my resources and a maintainable structure (for example one file per Template)?
For example: I am creating a WPF application and I want to use a TabControl, but I want to make major changes to it. So I could create a style in and apply it to the TabControl and TabItem. That's ok, but where can I place my resources to keep my Window XAML clear and have the style accessible from other Windows or projects as well?
I found that I can add it to App.xaml but that is only a solution for one project and allows sharing just between items of this project. Also, I think it would be better to have these templates a little separate from other code, than placing it all in some page or app.xaml?
I usually create a seperate styling project, which I reference from the projects, which I want to style. The styling project has a fixed structure like this:
For every control, I create a styling
ResourceDictionary
. For example for my buttons:In one main
ResourceDictionary
, I merge all the other dictionaries, in this case in the file IncaDesign.xaml, which you can see in the picture above:Notice how I defined the default styles, which are applied automatically, unless you specify otherwise. In every window or control, that you want to style, you only need to reference this one
ResourceDictionary
. Note the definition of the source, which is a reference to the assembly (/Commons.Styling;component...
)Default styles will be set automatically now, and if you want to access a resource explicitly, you can do this, using
StaticResource
.This is very nice solution in my opinion, which works for very complex solutions, including modular solutions, for example built with PRISM.
A
ResourceDictionary
is for what you are looking.Here is an explanation for how to use them within and across projects.
You can create a project that contains Resource Dictionaries, either in your solution or in a separate one. Your project will be a Class Library type and can then easily be .dll referenced from any other project. Here is an article describing this: Resource Dictionary Article