Thomas
Thomas

Reputation: 8013

Setting Data Property in Silverlight Path Style

I am trying to put as much properties of a Path element into a Style, this works out ok, as longs as I don't add Data to the Style setters:

<UserControl x:Class="Demo.Controls.SilverlightControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <Style x:Name="PathStyle" TargetType="Path">
                <Setter Property="Data" Value="0,0 L1,0"></Setter>
                <Setter Property="Stroke" Value="Blue"></Setter>     
                <Setter Property="Stretch" Value="Fill"></Setter>
            </Style>

        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Path Grid.Row="0"
               Height="7"               
               Data="M0,0 L1,0"
               Stretch="Fill"
               Stroke="Black"/>
        <Path Grid.Row="1"
               Height="7"               
               Style="{StaticResource PathStyle}"/>
    </Grid>
</UserControl>

If you open this example, you'll see that the first path gives no problems, but the second one results in an AG_E_UKNOWN_ERROR in Visual Studio 2008.

Is it possible to define the Data of a Path in a Style?

Upvotes: 2

Views: 2062

Answers (2)

Dave Haigh
Dave Haigh

Reputation: 4547

defining the data property within a style will only result in the first element that uses the style being rendered. the rest will not render. you need to do something like the following:

<Style x:Name="PathStyle" TargetType="Path"> 
<Setter Property="Stroke" Value="Blue"/> 
<Setter Property="Stretch" Value="Fill"/> 
</Style> 

<Path Style="{StaticResource PathStyle}" Data="M0,0 L1,0" /> 
<Path Style="{StaticResource PathStyle}" Data="M0,0 L1,0" /> 
<Path Style="{StaticResource PathStyle}" Data="M0,0 L1,0" /> ...

Upvotes: 0

user122069
user122069

Reputation: 419

This should work:

<Style x:Name="PathStyle" TargetType="Path">
    <Setter Property="Data" Value="M0,0 L1,0"/>
    <Setter Property="Stroke" Value="Blue"/>
    <Setter Property="Stretch" Value="Fill"/>
</Style>

Upvotes: 1

Related Questions