Image grid using ImageBrush

I want to use ImageBrush in XAML to apply the background to the Grid .

I gave a x:Key brushes and want to refer to it in my grid.

Unfortunately, the image does not appear 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 that I probably say this wrong, I tried DynamicResource and StaticResource .

+6
source share
3 answers

In your main grid, you have inner children that cover the entire available space of the outer grid, so you won’t be able to see the background.

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

only has the width, which is fine, but what about the height. if you just make the height bigger than your children, it will appear.

or better to have stock in internal children.

Margin = "5"

or make the inner child transparent like

Opacity = "0.2"

+2
source

I use this usually. If images are added to the project as a resource, refer to them like this.

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

And then point the image brush:

 <Border Background="{StaticResource play}"/> 
+9
source

I always did it like this:

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

Or, if you call it with the imagebrush resource, using an image path more similar to what paul suggested using StaticResource to call this style.

+3
source

Source: https://habr.com/ru/post/916459/


All Articles