使用ImageBrush的网格背景图像

时间:2012-05-23 14:04:07

标签: wpf image xaml background imagebrush

我希望在XAML中使用ImageBrush将背景应用于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>

我想我可能以错误的方式提到它,我已尝试DynamicResourceStaticResource

3 个答案:

答案 0 :(得分:10)

我经常使用这个。如果图像作为资源添加到项目中,请按照这样的方式引用它们。

<ImageBrush x:Key="play" ImageSource="../Images/Buttons/Play.png" />

然后引用图像画笔:

<Border Background="{StaticResource play}"/>

答案 1 :(得分:3)

我总是这样做;

<Grid>
   <Grid.Background>
      <ImageBrush ImageSource="/Resources/Images/BG_BlankOptimized.png"/>
   </Grid.Background>
</Grid>

或者如果使用图像路径通过图像刷资源调用它,更像是使用StaticResource调用该样式的paul建议。

答案 2 :(得分: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>

只有宽度可以,但高度怎么样。如果你的身高比你孩子的物品大,那就会出现。

或者更好地在孩子内部留有余量。

保证金= “10”

或让内在的孩子像

一样透明

不透明度= “0.2”

相关问题