【问题标题】:How can i set the background of a grid?如何设置网格的背景?
【发布时间】:2012-03-30 19:39:51
【问题描述】:

我正在玩弄样式,并想设置网格的背景,如下所示:

        <Style TargetType="Grid">
            <Setter Property="Background" Value="Background.png" />
        </Style>

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

【问题讨论】:

    标签: windows-phone-7 xaml


    【解决方案1】:

    你可以像这样直接设置背景属性。

    <Grid x:Name="ContentPanel" Style="{StaticResource GridStyle1}">
            <Grid.Background>
                <ImageBrush Stretch="Fill" ImageSource="/BackgroundImage.png"/>
            </Grid.Background>
    </Grid>
    

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

    <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}"/>
    

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

    【讨论】:

      【解决方案2】:

      你可以这样做

      <Grid x:Name="myGrid">
              <Grid.Background>
                  <ImageBrush Stretch="Fill" ImageSource="Assets/myimage.png"/>
              </Grid.Background>
      </Grid>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-12
        • 2017-12-08
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-14
        相关资源
        最近更新 更多