XAML resources is a object that is a collection of properties. A resource can be reused in different places of WPF application. The example of resources are mainly Styles and Brushes which are used in your WPF application. Resources can be declared on following places in a WPF application:
Application Level Resources:
WPF resources can be declared at application level in the App.xaml file (a global application scope within the application) i.e. <Application.Resources></Application.Resources>.
The example of Application Level Resources is
<Application.Resources>
<Style x:Key="mainHeadingTextStyle">
<Setter Property="Label.HorizontalAlignment" Value="Center"></Setter>
<Setter Property="Label.VerticalAlignment" Value="Top"></Setter>
<Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
<Setter Property="Label.FontWeight" Value="Bold"></Setter>
<Setter Property="Label.FontSize" Value="18"></Setter>
<Setter Property="Label.Foreground" Value="#0066cc"></Setter>
</Style>
</Application.Resources>
Window/Panel Level Resources:
WPF resources can also declare at Window/User Control level or declare them at panel level. i.e. within the Resources property of current window i.e. <Window.Resources></Window.Resources>, <Grid.Resources></Grid.Resources> or <StackPanel.Resources></StackPanel.Resources> .
The example of Window Level Resources is
<Window.Resources>
<SolidColorBrush x:Key="textHeadingForeground" Color="#0066cc"/>
</Window.Resources>
The example of Panel Level Resources is
<Grid.Resources>
<ImageBrush x:Key="backgroundImageBrush" ImageSource="images/watermark.png" />
</Grid.Resources>
FrameworkElement or FrameworkContentElement Level Resources:
WPF resources can also declare within the Resources property of any FrameworkElement or FrameworkContentElement i.e. <Button.Resources></Button.Resources>
Separate XAML resource file
WPF resources can also declare and refer resources in ResourceDictionary. To add a Resource Dictionary into your WPF applicaiton, right click the WPF project > add a Resource Dictionary. In my case it is "MyResourceDictionary.xaml"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MySampleWpfApplication.Resources">
<Style x:Key="mainHeadingTextStyle">
<Setter Property="Label.HorizontalAlignment" Value="Center"></Setter>
<Setter Property="Label.VerticalAlignment" Value="Top"></Setter>
<Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
<Setter Property="Label.FontWeight" Value="Bold"></Setter>
<Setter Property="Label.FontSize" Value="18"></Setter>
<Setter Property="Label.Foreground" Value="#0066cc"></Setter>
</Style>
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<Setter Property="Width" Value="125" />
<Setter Property="Height" Value="25" />
<Setter Property="Margin" Value="0,10,0,0" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</ResourceDictionary>
Once you declare the resource dictionary, all content of MyResourceDictionary.xaml refer them at application level i.e. App.xaml as shown below -
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
0 Comment(s)