<setter.value>可以在其中包含多个网格</setter.value>

时间:2010-05-13 05:48:14

标签: wpf silverlight silverlight-3.0

我想在App.XAML中为我的应用程序定义背景。之前的背景是在另一个xaml页面中定义的,其中包含多个网格,如下所示:

<Grid x:Key="GridGeneric" d:LayoutOverrides="Width, Height">
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF00172E" Offset="1"/>
                <GradientStop Color="#FF004074" Offset="0.433"/>
                <GradientStop Color="#FF081316"/>
                <GradientStop Color="#FF001D3F" Offset="0.215"/>
                <GradientStop Color="#FF002043" Offset="0.818"/>
                <GradientStop Color="#FF003B6C" Offset="0.642"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Grid>
            <Grid.Background>
                <RadialGradientBrush RadiusY="0.973" GradientOrigin="0.497,-0.276" RadiusX="1.003">
                    <GradientStop Color="#FFB350EE" Offset="0"/>
                    <GradientStop Color="#001D3037" Offset="0.847"/>
                </RadialGradientBrush>
            </Grid.Background>
        </Grid>

------
-----
</Grid>

现在我想把它放在我的App.xaml中,如下所示:

<Style x:Key="backgroundStyle" TargetType="Grid">

        <Setter Property="Background">
            <Setter.Value>
                <Grid>
                    <Grid.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF00172E" Offset="1"/>
                            <GradientStop Color="#FF004074" Offset="0.433"/>
                            <GradientStop Color="#FF081316"/>
                            <GradientStop Color="#FF001D3F" Offset="0.215"/>
                            <GradientStop Color="#FF002043" Offset="0.818"/>
                            <GradientStop Color="#FF003B6C" Offset="0.642"/>
                        </LinearGradientBrush>
                    </Grid.Background>
           <Grid>
             <Grid.Background>
                <RadialGradientBrush RadiusY="0.973" GradientOrigin="0.497,-0.276" RadiusX="1.003">
                        <GradientStop Color="#FFB350EE" Offset="0"/>
                        <GradientStop Color="#001D3037" Offset="0.847"/>
                    </RadialGradientBrush>
                </Grid.Background>
            </Grid>
                         ---------
                         ---------
                </Grid>
            </Setter.Value>
        </Setter>

    </Style>

但是这样做我得到以下例外。

alt text http://www.freeimagehosting.net/uploads/bb1ca0d3f6.jpg

1 个答案:

答案 0 :(得分:2)

您正尝试将网格设置为背景属性,这是不可能的。 background属性期望刷子,因此也是异常。

您可以使用DrawingBrush

实现所需目标

否则你必须在窗口中使用嵌套的网格布局并应用这样的两种样式

在您的窗口中绘制此布局:

<Grid x:Key="GridGeneric" Background="{StaticResource OuterGridBrush}"> 
    <Grid Background="{StaticResource InnerGridBrush}"> 
    </Grid> 
</Grid> 

将其放入app.xaml的资源部分:

<LinearGradientBrush x:Key="OuterGridBrush" EndPoint="0.5,1" StartPoint="0.5,0"> 
    <GradientStop Color="#FF00172E" Offset="1"/> 
    <GradientStop Color="#FF004074" Offset="0.433"/> 
    <GradientStop Color="#FF081316"/> 
    <GradientStop Color="#FF001D3F" Offset="0.215"/> 
    <GradientStop Color="#FF002043" Offset="0.818"/> 
    <GradientStop Color="#FF003B6C" Offset="0.642"/> 
</LinearGradientBrush> 


<RadialGradientBrush x:Key="InnerGridBrush" RadiusY="0.973" GradientOrigin="0.497,-0.276" RadiusX="1.003"> 
    <GradientStop Color="#FFB350EE" Offset="0"/> 
    <GradientStop Color="#001D3037" Offset="0.847"/> 
</RadialGradientBrush>