Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid Background Image using ImageBrush

I wish to use an ImageBrush in XAML to apply a background to a Grid.

I've given the brush a x:Key and want to refer to it in my grid.

Sadly, it doesn't come up with the image as a background at all.

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

I think I'm probably referring to it in the wrong way, I've tried DynamicResource and StaticResource.

like image 414
Luke Avatar asked May 23 '12 14:05

Luke


2 Answers

I use this commonly. If the images are added to the project as a Resource, reference them relatively like this.

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

And then reference the image brush:

<Border Background="{StaticResource play}"/>
like image 191
erodewald Avatar answered Nov 15 '22 09:11

erodewald


I always did it like this;

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

Or if calling it by an imagebrush resource using a an image path more like what paul suggested using StaticResource to call that style.

like image 38
Chris W. Avatar answered Nov 15 '22 10:11

Chris W.