Silverlight 4中的自定义网格样式

时间:2010-05-20 11:53:25

标签: silverlight coding-style silverlight-4.0 background

我想使用样式设置网格的背景。我风格我正在设置网格的背景属性。

但我有一个填充了LinearGradientFill的边框和一个也包含LinearGradientFill的Path。

但我无法将两者结合起来。

以下是示例代码。我想把它创建成一种风格。

<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>

它给我一个错误:

  

属性'价值'设置不止一次。

1 个答案:

答案 0 :(得分:4)

阿奇,

您需要使用模板才能将任意XAML放入样式中。不幸的是,只有控件有模板,而网格和边框不是控件。但有一个解决方案。虽然不如您所希望的那样干净,但以下XAMl应该可以实现您的目标。您将以下XAML粘贴到Charles Petzold's XAML Cruncher中以查看结果:

<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>