与背景图象和颜色的栅格

Jer*_*dev 12 grid xaml background image colors

是否可以在xaml中为背景图像和颜色提供整个网格?我没有缩放图像,因此有些区域没有颜色.是否可以用颜色为网格的其余部分着色?

这是我目前的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

Mar*_*all 9

我能想到的唯一方法是使用Background Property设置Color,然后将一个Image添加到Grid中,确保跨越所有的Rows和Columns.只要图像是网格中的第一个项目,其他项目就会叠加在顶部.我相信它会给你你想要的效果.

<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>
Run Code Online (Sandbox Code Playgroud)


Iri*_*son 9

在WPF中你甚至可以这样做,如果你想用VisualBrush定义一个带有png和颜色的画笔(这个画笔 - 由于渲染画笔时性能受到影响,很多其他画笔在Windows应用商店中不可用)一个基本的例子,画笔有很多你可以玩的属性:

    <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}"/>
Run Code Online (Sandbox Code Playgroud)


Vas*_*leF 7

您可以尝试在网格周围使用带有所需颜色集的边框作为背景颜色.

<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>
Run Code Online (Sandbox Code Playgroud)


Woo*_*aSh 1

你的意思是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 如果您设置背景属性两次,那是行不通的。 (11认同)