views:

1778

answers:

1

I am currently testing with Silverlight 2.0 Beta 2, and my goal is to define a resource element once and then reuse it many times in my rendering. This simple example defines a rectangle (myRect) as a resource and then I attempt to reuse it twice -- which fails with the error:

Attribute {StaticResource myRect} value is out of range. [Line: 9 Position: 83]

BTW, this sample works fine in WPF.

<UserControl x:Class="ReuseResourceTest.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="200" Height="200">
    <Canvas x:Name="LayoutRoot" Background="Yellow">
        <Canvas.Resources>
            <RectangleGeometry x:Key="myRect" Rect="25,50,25,50" />
        </Canvas.Resources>
        <Path Stroke="Black" StrokeThickness="10" Data="{StaticResource myRect}" />
        <Path Stroke="White" StrokeThickness="4"  Data="{StaticResource myRect}" />
    </Canvas>
</UserControl>

Any thoughts on what's up here.

Thanks,

-- Ed

+2  A: 

I have also encountered the same problem when trying to reuse components defined as static resources. The workaround I have found is not declaring the controls as resources, but defining styles setting all the properties you need, and instantiating a new control with that style every time you need.

EDIT: The out of range exception you are getting happens when you assign a control to a container that already is inside another container. It also happens in many other scenarios (such as applying a style to an object that already has one), but I believe this is your case.

Santiago Palladino