如何设置网格的背景?

Jas*_*n94 1 xaml windows-phone-7

我正在玩风格,并想要设置网格的背景,如下所示:

        <Style TargetType="Grid">
            <Setter Property="Background" Value="Background.png" />
        </Style>
Run Code Online (Sandbox Code Playgroud)

但是这不起作用,正确的方法是什么......我怎么能这样做,就像我用css中的类做的那样,因为我希望它影响每一个Grid,一个包装页面?

kin*_*ple 7

您可以直接设置背景属性.

<Grid x:Name="ContentPanel" Style="{StaticResource GridStyle1}">
        <Grid.Background>
            <ImageBrush Stretch="Fill" ImageSource="/BackgroundImage.png"/>
        </Grid.Background>
</Grid>
Run Code Online (Sandbox Code Playgroud)

如果要创建样式资源,可以像这样设置值

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="GridStyle1" TargetType="Grid">
        <Setter Property="Background">
            <Setter.Value>
                <ImageBrush ImageSource="/BackgroundImage.png" Stretch="Fill"/>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

<Grid Style="{StaticResource GridStyle1}"/>
Run Code Online (Sandbox Code Playgroud)

我建议使用Expression Blend来帮助您了解如何使用样式.它将为您生成控件模板,以便您可以看到它们的结构.