【发布时间】:2012-05-30 02:23:03
【问题描述】:
我希望在 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>
我想我可能指错了,我试过DynamicResource和StaticResource。
【问题讨论】:
-
您是如何在项目中包含背景图片的?如果它包含为“内容”,那么我希望您的 ImageSource 看起来更像这样:
ImageSource="pack://application:,,,/component/images/bird_text_bg.jpg" -
如果您直接指定它(而不是使用资源)它会显示吗?
-
这很奇怪。我将其设置为内容并在 Visual Studio 中有图像。我只是在属性窗格中为“ImageSource”使用了 Visual Studio 中的椭圆按钮,然后它自动为我生成了该路径。
-
不,如果我直接引用图像
c:/myfolder/bird_text_bg.jpg它也不会出现。
标签: wpf image xaml background imagebrush