5

I'm starting to develop a Desktop application using WPF (.net 3.5 sp1, with VS only, not blend as yet).

I'm at the point were I have some generic reusable components in several libraries.

Where can I define my Styles & Data Templates so that they are reusable across several projects, so I can have a consistent look and feel?

I've looked at ResourceDictionaries, but am unsure that

  1. They're what I need
  2. If they are what I need, how I can 'import' them into other project and reference their contents in Xaml.

Thanks,

3 Answers 3

10

ResourceDictionary is the way to go, you can either copy an xaml file containing the resource dictionary between projects or compile them into a DLL you'll reference from your projects.

To reference dictionaries in the same project you add something like this to your App.xaml (in this case I keep my resources in the ControlStyles folder).

<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ControlStyles/Colors.xaml"/> <ResourceDictionary Source="ControlStyles/Window.xaml"/> <ResourceDictionary Source="ControlStyles/Button.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 

If you compile them into a different dll you can use this syntax (if the styles dll is called StyleAssembly, the word "component" is actually part of the syntax and not a directory name):

<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Colors.xaml"/> <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Window.xaml"/> <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Button.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 
Sign up to request clarification or add additional context in comments.

Comments

3

@Nir is right, the only thing I like to do as well is to replace

 <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/ControlStyles/Colors.xaml"/> 

with this shorthand

 <ResourceDictionary Source="/StyleAssembly;component/ControlStyles/Colors.xaml"/> 

It just looks cleaner to me and the runtime will prefix pack://application:,,, when it tries to locate the resource.

Comments

0

You want merged resource dictionaries, as described here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.