Custom grid style in Silverlight 4

I want to set the background of a grid using style. I am styled, I am setting the background property of the grid.

But I have a border filled with LinearGradientFill and a Path that also contains a LinearGradientFill.

But I cannot combine both.

Below is a 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>

      

This gives me an error like:

The Value property is set more than once.

+2


a source to share


1 answer


Archie,

You need to use a template to place arbitrary XAML in style. Unfortunately, only controls have templates, and grids and borders are not controls. But there is a solution. While not as clean as you'd like, the following XAMl should do your job. You paste the following XAML into Charles Petzold 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>

      

+4


a source







All Articles