Resource sharing in Silverlight 2 isn't as easy as in WPF - it only works for certain resources such as brushes, and that's not goint to change in Silverlight 3 either. For example, if you want to reuse geometry paths you will get an error:

<UserControl x:Class="PaulTest.Page"



Width="400" Height="400">

<Canvas x:Name="LayoutRoot" Background="Yellow">


<Style x:Key="Black" TargetType="Path">

<Setter Property="Stroke" Value="Black" />

<Setter Property="StrokeThickness" Value="10" />


<Style x:Key="White" TargetType="Path">

<Setter Property="Stroke" Value="White" />

<Setter Property="StrokeThickness" Value="2" />

<Setter Property="StrokeDashArray" Value="1,1,0" />


<RectangleGeometry x:Key="Geom" Rect="25,25,100,100" />


<Path x:Name="One" Style="{StaticResource Black}" Data="{StaticResource Geom}" />

<Path x:Name="Two" Style="{StaticResource White}" Data="{StaticResource Geom}" />



Silverlight team is aware of the problem but decided not to address it for now for performance reasons, apparently it would require additional overhead when storing temporary objects while constructing the object tree. But any resource that can be newed up is still good to go. Obviously, the resource can always be newed up in code behind but that would defeat the purpose of having the markup and code separation, would it?

Here's a solution that is generic enough. It utilizes Silverlight’s data binding features to grab a value of geometry structure (for example Rect for RectangleGeometry) and perform on-the-fly conversion of them into new Geometry instances. Only one new class has to be created for each Geometry class (e.g.  RectangleGeometry) or one generic parameterized converter class that would take and structure parameter and return a corresponding Geometry instance.

XAML should include reference to a converter class instance in any Resources section:


<local:RectConverter x:Key="RectConverter1" />


where ‘local’ refers to a namespace where converter class is declared:


namespace SilverlightApplication1


       public class RectConverter : IValueConverter


              public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)


                     RectangleGeometry rg = new RectangleGeometry();

                     rg.Rect = (Rect) value;


                     return rg;



              public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)


                     throw new NotImplementedException("This Convert supports only OneWay binding");





Now, in our XAML we should only change very little to support 'Rect1' resource sharing each Path element:

<Path x:Name="One" Style="{StaticResource Black}" Data="{Binding Path=Rect, Source={StaticResource Rect1}, Mode=OneWay, Converter={StaticResource RectConverter1}, ConverterParameter=null}" />