What you're looking for is ResourceDictionary. This is much more flexible than just putting styles in your App.Resources Element and gives you much more control of the scope of your styles.
Putting Styles in your App.Resources has a number of disadvantages:
- It can fill up really quickly and turn into a large, bloated list
- Every style there is globally available. You may not want that.
Using A ResourceDictionary largely fixes this:
- Styles can be kept in one or more assemblies and be re-used across appplications
- By including resourcedictionaries (or not) you can control what styles are added to a page
- You can group and organise your styles and templates in a way that is logical to you
A resource dictionary maps pretty closely to a CSS file. Basically, you use these to store a mix of things:
- Styles
- ControlTemplates and DataTemplates
- Brushes, etc
And, like stylesheets you can apply them across entire control types or to controls that use the named style
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Styles/DialogStyles.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Icons/Error.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Icons/Exit.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Project.Ui;component/Icons/Warning.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Definining a ResourceDictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Infrastructure="clr-namespace:Hsbc.Ice.Shell.Infrastructure"
xmlns:Ui="clr-namespace:Hsbc.Ice.Shell.Infrastructure.Ui">
<LinearGradientBrush x:Key="{x:Static Ui:Brushes.SelectedRowBackgroundBrushKey}" StartPoint="0.5,0" EndPoint="0.5,1"
po:Freeze="True">
<GradientStop Color="#4D5F6E96" Offset="0"/>
<GradientStop Color="#2191A0BE" Offset="0.2"/>
<GradientStop Color="#2191A0BE" Offset="0.45"/>
<GradientStop Color="#745F6E96" Offset="1"/>
</LinearGradientBrush>
</ResourceDictionary>