使用ImageBrush的网格背景图像

Luk*_*uke 6 wpf xaml background image imagebrush

我希望ImageBrush在XAML中使用一个背景来应用Grid.

我已经给了画笔,x:Key并希望在我的网格中引用它.

可悲的是,它根本没有提供图像作为背景.

<Window.Resources>
    <ImageBrush ImageSource="/MAQButtonTest;component/images/bird_text_bg.jpg" x:Key="BackgroundSponge" />
    <Style TargetType="TextBlock">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
    </Style>
    <ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
        <Grid Width="444" ShowGridLines="False" SnapsToDevicePixels="True" Background="{DynamicResource BackgroundSponge}">
            <Grid.RowDefinitions>
                <RowDefinition Height="51" />
                <RowDefinition Height="36" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Background="#286c97">

            </Grid>
            <Grid Grid.Row="1" Background="#5898c0">
                <ContentPresenter Grid.Row="0" />
            </Grid>
        </Grid>
    </ControlTemplate>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

我想我大概指的是它在错误的道路,我试过DynamicResourceStaticResource.

ero*_*ald 10

我常用这个.如果图像作为资源添加到项目中,请像这样引用它们.

<ImageBrush x:Key="play" ImageSource="../Images/Buttons/Play.png" />
Run Code Online (Sandbox Code Playgroud)

然后参考图像画笔:

<Border Background="{StaticResource play}"/>
Run Code Online (Sandbox Code Playgroud)


JSJ*_*JSJ 2

在您的主网格中,您有内部子网格,它们覆盖了外部网格的所有可用空间,这就是您看不到背景的原因。

 <Grid Width="444"
          Height="500" 
          Background="{DynamicResource BackgroundSponge}"
          ShowGridLines="False"
          SnapsToDevicePixels="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="51" />
            <RowDefinition Height="36" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Background="#286c97"  Opacity="0.2" Margin="5"/>
        <Grid Grid.Row="1" Background="#5898c0" Opacity="0.2" Margin="5">
            <ContentPresenter Grid.Row="0" />
        </Grid>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

只有宽度还可以,但是高度呢?如果您只是将高度设置得比您的孩子项目更大,它就会显示出来。

或者更好的是在内部孩子中留有余量。

保证金=“5”

或者让内心的孩子变得透明,比如

不透明度=“0.2”