In Windows Presentation Foundation (WPF), a ResourceDictionary is a container that holds resources used by WPF applications.
These resources can include styles, templates, brushes, and other objects
that can be reused throughout an application.
By using a ResourceDictionary, developers can manage and organize these resources in a centralized and reusable manner.
A ResourceDictionary allows you to define resources in one place and reference them throughout your application
, promoting
reusability and maintainability.
Resources defined in a ResourceDictionary can be shared across multiple elements
or even multiple applications if the dictionary
is made available globally.
Resource dictionaries are often defined in XAML
, making it easy to visually manage resources. They can be defined inline within
a XAML file or in separate XAML files for better organization.
Resources can be referenced dynamically using the DynamicResource markup extension
, allowing for changes in the resource values
to be automatically updated in the UI
.
Multiple resource dictionaries can be combined using merged dictionaries
, enabling the organization of resources into separate
files and then combining them into a single dictionary.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><SolidColorBrush x:Key="PrimaryBrush" Color="Blue"/><Style x:Key="ButtonStyle" TargetType="Button"><Setter Property="Background" Value="{StaticResource PrimaryBrush}"/><Setter Property="Foreground" Value="White"/><Setter Property="FontSize" Value="16"/></Style></ResourceDictionary>
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Resources.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Window.Resources><Grid><Button Style="{StaticResource ButtonStyle}" Content="Click Me" Width="100" Height="50"/></Grid></Window>
In WPF, StaticResource and DynamicResource are two ways to reference resources from a ResourceDictionary. They differ in how and when the resource values are resolved and applied to UI elements.
compile time or load time
.better performance
.do not change during the application's lifetime
, such as colors, styles, and templates
that remain constant.<Button Background="{StaticResource PrimaryBrush}" Content="Static Resource"/>
the resource reference at runtime
.performance overhead compared
to StaticResource.change during the application's lifetime
, such as themes or user settings
that can be
modified while the application is running.In WPF, styles are a powerful way to define a consistent look and feel for your controls. A style is a collection of property setters that apply to controls of a specific type. You can define styles in various scopes such as in a ResourceDictionary, within a Window or UserControl’s resources, or globally at the application level.
Styles are usually defined in XAML. A style consists of a TargetType
(the control type it applies to) and a collection of Setter elements,
each of which sets a property to a specific value
.
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><Style x:Key="ButtonStyle" TargetType="Button"><Setter Property="Background" Value="Blue"/><Setter Property="Foreground" Value="White"/><Setter Property="FontSize" Value="16"/><Setter Property="Margin" Value="5"/></Style></Window.Resources><Grid><Button Style="{StaticResource ButtonStyle}" Content="Click Me"/><Button Style="{StaticResource ButtonStyle}" Content="Press Me" HorizontalAlignment="Right"/></Grid></Window>
You can define a style without an x:Key
attribute, which makes it an implicit style. Implicit styles are automatically applied to all controls
of the specified TargetType within the scope of the resource dictionary.
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><Style TargetType="Button"><Setter Property="Background" Value="Blue"/><Setter Property="Foreground" Value="White"/><Setter Property="FontSize" Value="16"/><Setter Property="Margin" Value="5"/></Style></Window.Resources><Grid><Button Content="Click Me"/><Button Content="Press Me" HorizontalAlignment="Right"/></Grid></Window>
You can base one style on another using the BasedOn
property, which allows for style inheritance.
In WPF, triggers are a way to change the appearance or behavior of a control
based on certain conditions or events.
Triggers allow you to define styles, animations, or property changes
that get applied dynamically when a specified
condition is met. There are several types of triggers in WPF, each suited for different scenarios:
Example: Change Button Background on Mouse Over:
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><Style TargetType="Button"><Setter Property="Background" Value="LightGray"/><Setter Property="Foreground" Value="Black"/><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Blue"/><Setter Property="Foreground" Value="White"/></Trigger></Style.Triggers></Style></Window.Resources><Grid><Button Content="Hover over me"/></Grid></Window>
Example: Start Animation on Button Click:
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Grid><Button Content="Click me" Width="100" Height="50"><Button.Triggers><EventTrigger RoutedEvent="Button.Click"><BeginStoryboard><Storyboard><DoubleAnimation Storyboard.TargetProperty="Width" To="200" Duration="0:0:1"/></Storyboard></BeginStoryboard></EventTrigger></Button.Triggers></Button></Grid></Window>
Example: Change TextBlock Foreground Based on Data:
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.DataContext><local:ViewModel/></Window.DataContext><Grid><TextBlock Text="{Binding Status}" FontSize="20"><TextBlock.Style><Style TargetType="TextBlock"><Style.Triggers><DataTrigger Binding="{Binding Status}" Value="Error"><Setter Property="Foreground" Value="Red"/></DataTrigger><DataTrigger Binding="{Binding Status}" Value="OK"><Setter Property="Foreground" Value="Green"/></DataTrigger></Style.Triggers></Style></TextBlock.Style></TextBlock></Grid></Window>
Example: Change Button Style Based on Multiple Properties:
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><Style TargetType="Button"><Setter Property="Background" Value="LightGray"/><Setter Property="Foreground" Value="Black"/><Style.Triggers><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsMouseOver" Value="True"/><Condition Property="IsPressed" Value="True"/></MultiTrigger.Conditions><Setter Property="Background" Value="DarkBlue"/><Setter Property="Foreground" Value="White"/></MultiTrigger></Style.Triggers></Style></Window.Resources><Grid><Button Content="Press me"/></Grid></Window>
Example: Change TextBlock Style Based on Multiple Data Conditions:
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.DataContext><local:ViewModel/></Window.DataContext><Grid><TextBlock Text="{Binding Status}" FontSize="20"><TextBlock.Style><Style TargetType="TextBlock"><Style.Triggers><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding Status}" Value="Error"/><Condition Binding="{Binding IsCritical}" Value="True"/></MultiDataTrigger.Conditions><Setter Property="Foreground" Value="DarkRed"/></MultiDataTrigger></Style.Triggers></Style></TextBlock.Style></TextBlock></Grid></Window>
Attached properties in WPF are a special type of dependency property that allows a property to be set on an object that does not directly
define that property. They are typically used to enable child elements to inform their parent elements how they should be treated or
how they should behave
.
<Canvas><Button Content="Button 1" Canvas.Left="50" Canvas.Top="20"/><Button Content="Button 2" Canvas.Left="150" Canvas.Top="50"/></Canvas>
Define the Attached Property in a Static Class:
public static class CustomProperties{public static readonly DependencyProperty IsHighlightedProperty =DependencyProperty.RegisterAttached("IsHighlighted",typeof(bool),typeof(CustomProperties),new PropertyMetadata(false));public static bool GetIsHighlighted(UIElement element){return (bool)element.GetValue(IsHighlightedProperty);}public static void SetIsHighlighted(UIElement element, bool value){element.SetValue(IsHighlightedProperty, value);}}
Use the Custom Attached Property in XAML:
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp"Title="MainWindow" Height="350" Width="525"><Grid><Button Content="Highlight Me" local:CustomProperties.IsHighlighted="True"/></Grid></Window>
Quick Links
Legal Stuff
Social Media