Search code examples
silverlightcoding-stylesilverlight-4.0background

Custom grid style in Silverlight 4


I want to set background of a grid using a style. I style I'm setting the Background Property of the grid.

But I have a border filled with LinearGradientFill and a Path which also has LinearGradientFill in it.

But I'm not able to combine both.

Below is sample code. I want to create it as a style.

<Grid>
<Border BorderBrush="Black" BorderThickness="2">
                <Border.Background>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="Black" Offset="0.953" />
                        <GradientStop Color="White" Offset="0" />
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
            <Path Data="M 0,0 C 0,620 10,10 560,0" Height="60" VerticalAlignment="Top">
                <Path.Fill>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="Black" Offset="0" />
                        <GradientStop Color="White" Offset="0.779" />
                    </LinearGradientBrush>
                </Path.Fill>
            </Path>
</Grid>

It gives me an error as:

The Property 'Value' is set more than once.


Solution

  • Archie,

    You need to use a Template in order to place arbitrary XAML into a Style. Unfortuately, only Controls have Templates, and Grids and Borders are not Controls. But there is a solution. Although not as clean as you would like, the following XAMl should accomplish your goal. You paste the following XAML into Charles Petzold's XAML Cruncher to see the results:

    <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 Width="400" Height="400">
        <UserControl.Resources>
            <!-- A ContentControl template that defines your background -->
            <ControlTemplate x:Key="BackgroundTemplate" TargetType="ContentControl">
                <Grid>
                    <Border BorderBrush="Black" BorderThickness="2">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                <GradientStop Color="Black" Offset="0.953" />
                                <GradientStop Color="White" Offset="0" />
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                    <Path Data="M 0,0 C 0,620 10,10 560,0" Height="60" VerticalAlignment="Top">
                        <Path.Fill>
                            <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                <GradientStop Color="Black" Offset="0" />
                                <GradientStop Color="White" Offset="0.779" />
                            </LinearGradientBrush>
                        </Path.Fill>
                    </Path>
                </Grid>
            </ControlTemplate>
            <!-- A ContentControl Style that references the background template -->
            <Style x:Key="BackgroundStyle" TargetType="ContentControl">
                <Setter Property="Template" Value="{StaticResource BackgroundTemplate}" />
            </Style>
        </UserControl.Resources>
    
        <!-- Typical usage; place the background ContentControl behind your body content -->
        <Grid x:Name="LayoutRoot">
            <ContentControl Style="{StaticResource BackgroundStyle}" />
            <TextBlock Text="Your Content" Foreground="Red" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
    </UserControl>