Grid with background image and color

2019-02-12 06:17发布

问题:

Is it possible to give an entire grid in xaml both a background image and a color? I am not scaling the image so there are regions that do not have a color. Is it possible to color the rest of the grid in a color?

This is my current code:

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

回答1:

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>


回答2:

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


回答3:

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>


回答4:

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>


回答5:

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.



回答6:

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