
Understanding Logical Resources
Silverlight also supports logical resources (also known as object resources, declarative resources, or simply
resources). Logical resources represent any named .NET object embedded within an assembly. Logical resources are
extremely helpful when working with graphical data of any sort, given that you can define commonly used graphic
primitives (brushes, pens, animations, and more) for use in your programs. Logical resources are also very useful
when you are working with data templates, where you apply custom UIs to data-binding operations.
Every descendent of FrameworkElement supports a Resources property. This property encapsulates a
ResourceDictionary object that contains the defined resources. The ResourceDictionary can hold any type of item
as it operates on System.Object types. The ResourceDictionary can be manipulated via XAML or procedural code.
The UserControl class extends FrameworkElement. Also, all Silverlight UI controls extend FrameworkElement.
Therefore, all UI controls support a Resource property.
Assume you wish to define a custom LinearGradientBrush in markup, which needs to be accessed throughout a
StackPanel. To do so, populate the ResourceDictionary of the StackPanel using property-element syntax.Each
member in the resource dictionary is given a unique name via the x:Key attribute. When a nested element wishes to
access the object resource, make use of the {StaticResource} markup extension.
<UserControl x:Class="SLResourcesGraphicsAnim.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200" Height="200">
<!-- Notice that the stack panel defines the resource. -->
<StackPanel Background="White">
<StackPanel.Resources>
<LinearGradientBrush x:Key = "MyBrush" StartPoint = "0,0.5" EndPoint =
"1,0.5">
<GradientStop Color = "Green" Offset = "0.0" />
<GradientStop Color = "Orange" Offset = "0.25" />
<GradientStop Color = "Yellow" Offset = "0.75" />
<GradientStop Color = "Blue" Offset = "1.0" />
</LinearGradientBrush>
</StackPanel.Resources>
<Button Name = "btnOne" Height = "50" Width = "100">
<Button.Content>
<StackPanel Orientation = "Horizontal">
<TextBlock Text = "OK"/>
<Ellipse Height = "40" Width = "50" Fill = "{StaticResource MyBrush}"/>
</StackPanel>
</Button.Content>
</Button>
<Button Height = "40" Width = "50"
Background = "{StaticResource MyBrush}"/>
</StackPanel>
</UserControl>
Currently, only items defined within the StackPanel can access the brush resource. If you wish to make the resource
usable throughout the UserControl, package the logical resource within the <UserControl> scope.
<UserControl x:Class="SLResourcesGraphicsAnim.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200" Height="200">
<UserControl.Resources>
<LinearGradientBrush x:Key = "MyBrush" StartPoint = "0,0.5" EndPoint =
"1,0.5">
<GradientStop Color = "Green" Offset = "0.0" />
<GradientStop Color = "Orange" Offset = "0.25" />
<GradientStop Color = "Yellow" Offset = "0.75" />
<GradientStop Color = "Blue" Offset = "1.0" />
</LinearGradientBrush>
</UserControl.Resources>
<StackPanel Background="White">
<Button Name = "btnOne" Height = "50" Width = "100">
<Button.Content>
<StackPanel Orientation = "Horizontal">
<TextBlock Text = "OK"/>
<Ellipse Height = "40" Width = "50"
Fill = "{StaticResource MyBrush}"/>
</StackPanel>
</Button.Content>
</Button>
<Button Height = "40" Width = "50"
Background = "{StaticResource MyBrush}"/>
</StackPanel>
</UserControl>
Logical Resources
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services