Grid with background image and color

泪湿孤枕 提交于 2019-11-30 17:58:05

The only way that I can think of is to use the Background Property to set the Color, then add an Image to the Grid making sure to span all of your Rows and Columns. As long as the Image is the first Item in your grid the others will be layered on top. I beleive it will give you the effect you are looking for.

<Grid Background="Red">
    <Image Grid.RowSpan="2" Stretch="None" Source="Images/background_top.png" VerticalAlignment="Top" HorizontalAlignment="Center"/>
    <Label Content="Label" Grid.Row="0" Height="28" HorizontalAlignment="Center"  Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" />
    <Grid.RowDefinitions>
        <RowDefinition Height="50*" />
        <RowDefinition Height="50*" />
    </Grid.RowDefinitions>
</Grid>

In WPF you can even do this if you want to define a brush with both a png and color with VisualBrush (this brush - and quite a few other brushes are not available in Windows Store Apps due to performance hit when rendering the brushes) Here is a basic example , the brush has quite a few properties you can play around with:

    <Window.Resources>
    <VisualBrush x:Key="myBrush">
        <VisualBrush.Visual>
            <Grid>
                <Rectangle Fill="Red"/>
                <Image Source="troll.png"/>
            </Grid>
        </VisualBrush.Visual>
    </VisualBrush>
</Window.Resources>
<Grid Background="{StaticResource myBrush}"/>

You could try using a border with your desired colour set as background color, around the grid.

<Border Background="Red">
    <Grid>
        <Grid.Background>
            <ImageBrush Stretch="None" ImageSource="Images/background_top.png" AlignmentY="Top" AlignmentX="Center"/>
        </Grid.Background>

        <Grid.RowDefinitions>
            <RowDefinition Height="50*" />
            <RowDefinition Height="50*" />
        </Grid.RowDefinitions>
    </Grid>
</Border>
WooCaSh

You mean something like this:

<Grid Background="Red">
        <Grid.Background>
            <ImageBrush Stretch="None" ImageSource="Images/background_top.png" AlignmentY="Top" AlignmentX="Center"/>
        </Grid.Background>
    <Grid.RowDefinitions>
        <RowDefinition Height="50*" />
        <RowDefinition Height="50*" />
    </Grid.RowDefinitions>
</Grid>

Not exactly the answer to your question, but to obtain a similar visual effect, you could set the background of your grid to an image ; and the background of your Page/Window to a color.

Put a grid within another grid. The outer grid has a SolidColorBrush, and the inner grid has a partially transparent ImageBrush.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!