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
Thanks,
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.
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.
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.
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>
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With