wpf 使用 ImageBrush 的网格背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10721603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grid Background Image using ImageBrush
提问by Luke
I wish to use an ImageBrush
in XAML to apply a background to a Grid
.
我希望使用ImageBrush
XAML 中的 将背景应用于Grid
.
I've given the brush a x:Key
and want to refer to it in my grid.
我已经给了画笔 ax:Key
并想在我的网格中引用它。
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
.
我想我可能用错误的方式来指代它,我已经尝试过DynamicResource
并且StaticResource
.
采纳答案by JSJ
In your main grid you have inner childs which cover all the available space of outer grid thats why you wont 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>
is having only width which is ok but what about the height. if your just make height larger then your child items it will shows up.
只有宽度可以,但是高度呢。如果您只是使高度变大,那么您的子项目就会显示出来。
or better to have margin in inside childs.
或者更好地在内部孩子中有余量。
Margin="5"
边距 =“5”
or make inner child transparent like
或者让内心的孩子透明像
Opacity="0.2"
不透明度=“0.2”
回答by erodewald
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}"/>
回答by Chris W.
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.
或者,如果通过使用图像路径的图像刷资源调用它,更像是保罗建议使用 StaticResource 调用该样式的内容。