views:

71

answers:

2

I am trying to reuse shape data to dynamically create new shapes in code. Basically I want to do this http://stackoverflow.com/questions/839843/reuse-path-object-in-xaml but in code. Here is the situation:

        Path path = new Path();
        path.Style = (System.Windows.Style)this.Resources["PathStyle"];
        Path path2 = new Path();
        path2.Style = (System.Windows.Style)this.Resources["PathStyle"];

But i get an ArgumentException on the fourth line saying "Value does not fall within the expected range"

The code is in the MainControl of a SL 3 application. The XAML code for the resource is

 <UserControl.Resources>
        <Style x:Key="PathStyle" TargetType="Path">
            <Setter Property="Data" Value="M63,171 L138,117 L168,189 L99,219"/>
            <Setter Property="Fill" Value="#FFF07E7E"/>
            <Setter Property="Height" Value="103"/>
            <Setter Property="Width" Value="106"/>
            <Setter Property="Stroke" Value="#FF000000"/>
            <Setter Property="Stretch" Value="Fill"/>
        </Style>
</UserControl.Resources>

Any ideas?

A: 

That's weird... I can't see why it would work the first time and fail the second time. Try to use an intermediate variable instead of accessing Resources twice :

    Style style = (System.Windows.Style)this.Resources["PathStyle"];
    Path path = new Path();
    path.Style = style;
    Path path2 = new Path();
    path2.Style = style;
Thomas Levesque
i'm still getting the same error.
Raul ANDRISAN
+1  A: 

It seems that truly this is a bug in the SL3 Beta, but somebody has found a way around it:

http://tozon.info/blog/post/2009/06/27/Countdown-to-Silverlight-3-2-Setting-styles.aspx

It will no longer be a problem from July 12th when SL 3 hits RTM.

Raul ANDRISAN