Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML: How to define Data Templates & Styles to be used over several Projects

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,

like image 896
Binary Worrier Avatar asked Jun 10 '09 20:06

Binary Worrier


People also ask

What is a data template in WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.

What is difference between ItemTemplate and DataTemplate?

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.

Which of the following template describes the visual structure of a data object?

Use a DataTemplate to define the appearance of each of your data objects. The content of your DataTemplate becomes the visual structure of your data objects.


2 Answers

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>
like image 175
Nir Avatar answered Nov 15 '22 10:11

Nir


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

like image 26
Marthinus Avatar answered Nov 15 '22 12:11

Marthinus