Say that you have one resource that defines colors, for example:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Color A="#FF" R="#FF" G="#22" B="#11" x:Key="MyRed"/> <Color A="#FF" R="#00" G="#FF" B="#21" x:Key="MyGreen"/> <Color A="#FF" R="#00" G="#22" B="#FF" x:Key="MyBlue" /> <SolidColorBrush x:Key="MyGreenBrush" Color="{StaticResource MyGreen}"/> <SolidColorBrush x:Key="MyRedBrush" Color="{StaticResource MyRed}"/> <SolidColorBrush x:Key="MyBlueBrush" Color="{StaticResource MyBlue}"/> </ResourceDictionary>
And one more that defines some basic styles:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="{x:Type TextBlock}" x:Key="PocTextBlock"> <Setter Property="FontSize" Value="16"/> </Style> <Style TargetType="{x:Type TextBox}" x:Key="MyTextBox"> <Setter Property="FontSize" Value="20"/> <Setter Property="Foreground" Value="{DynamicResource MyGreenBrush}"/> </Style> <Style TargetType="{x:Type TextBlock}" x:Key="MyResultTextBlock"> <Setter Property="FontSize" Value="16"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Foreground" Value="{DynamicResource MyGreenBrush}"/> </Style> <Style TargetType="{x:Type Border}" x:Key="MyBorder"> <Setter Property="BorderBrush" Value="{DynamicResource MyGreenBrush}"/> <Setter Property="BorderThickness" Value="4"/> <Setter Property="CornerRadius" Value="5"/> </Style> </ResourceDictionary>
You can then add your resources to the App.xaml Application.Resources tag, as shown here:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="OtherStyles.xaml"/> <ResourceDictionary Source="Colors.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
Then in all of your UserControls you can use styles or brushes as StaticResources, as the sample code shows.
Espen medbΓΈ
source share