This article will show how to use resources in WPF.
There are two types of resources; they are:
- Static Resources
- Dynamic Resources
Static Resources: A Static Resource will be resolved and assigned to the property during the loading of the XAML that occurs before the application is actually run. It will only be assigned once and any changes to the resource dictionary is ignored.
Static resource references work best for the following circumstances:
- Your application design concentrates most of its resources into page or application level resource dictionaries. Static resource references are not re-evaluated based on runtime behaviours such as reloading a page, and therefore there can be some performance benefit to avoiding large numbers of dynamic resource references when they are not necessary per your resource and application design.
- You are setting the value of a property that is not on a Dependency Object or a freezable.
- You are creating a resource dictionary that will be compiled into a DLL, and packaged as part of the application or shared between applications.
- You are creating a theme for a custom control, and are defining resources that are used within the themes. For this case, you typically do not want the dynamic resource reference lookup behaviour; you instead want the static resource reference behaviour so that the lookup is predictable and self-contained in the theme. With a dynamic resource reference, even a reference within a theme is left unevaluated until runtime, and there is a chance that when the theme is applied, some local element will redefine a key that your theme is trying to reference, and the local element will fall prior to the theme itself in the lookup. If that happens then your theme will not behave in an expected manner.
- You are using resources to set large numbers of dependency properties. Dependency properties have effective value caching as enabled by the property system, so if you provide a value for a dependency property that can be evaluated at load time then the dependency property does not need to check for a re-evaluated expression and can return the last effective value. This technique can be a performance benefit.
- You want to change the underlying resource for all consumers, or you want to maintain separate writable instances for each consumer using the x: Shared Attribute.
Example:
<Window.Resources>
<SolidColorBrush x:Key="MyBrush" Color="Gold"/>
<Style TargetType="Border" x:Key="PageBackground">
<Setter Property="Background" Value="black"/>
</Style>
<Style TargetType="TextBlock" x:Key="TitleText">
<Setter Property="Background" Value="Green"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="#4E87D4"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="Margin" Value="0,40,10,10"/>
<Setter Property="Width" Value="300"></Setter>
</Style>
<Style TargetType="TextBlock" x:Key="Label">
<Setter Property="DockPanel.Dock" Value="Right"/>
<Setter Property="FontSize" Value="8"/>
<Setter Property="Foreground" Value="{StaticResource MyBrush}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Margin" Value="0,3,10,0"/>
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="20"></Setter>



Join the conversation! Your thoughts help the community grow.