开发者

Can <Setter.Value> have multiple grids inside it

开发者 https://www.devze.com 2022-12-29 22:57 出处:网络
I want to define the background for my application in App.XAML. The background was previously defined in another xaml page,which have multiple Grids inside it like following:

I want to define the background for my application in App.XAML. The background was previously defined in another xaml page,which have multiple Grids inside it like following:

<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>
                <RadialGradi开发者_如何学PythonentBrush 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>

Now I want to place the same in my App.xaml like following:

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

But While doing this I am getting the following Exception.

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


You're trying to set a grid to a background property, which is not possible. The background property will expect a brush and hence the exception.

You can achieve what you want using a DrawingBrush

Else you'll have to use your nested grid layout in the window and apply the two styles like this

Draw this layout in your window:

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

Drop this into your app.xaml's resources section:

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

精彩评论

暂无评论...
验证码 换一张
取 消