【问题标题】:Grid Background Image using ImageBrush使用 ImageBrush 的网格背景图像
【发布时间】:2012-05-30 02:23:03
【问题描述】:

我希望在 XAML 中使用 ImageBrushGrid 应用背景。

我已经给画笔一个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

【问题讨论】:

  • 您是如何在项目中包含背景图片的?如果它包含为“内容”,那么我希望您的 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


【解决方案1】:

我一直都是这样的;

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

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

【讨论】:

    【解决方案2】:

    我经常使用这个。如果图像作为资源添加到项目中,则相对像这样引用它们。

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

    然后引用图片刷:

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

    【讨论】:

    • 我要补充一点,对我来说,最重要的部分是“如果图像作为资源添加到项目中”部分,因为在我的情况下,它们是内容,这就是我有一个问题。谢谢!
    【解决方案3】:

    在您的主网格中,您的内部子节点覆盖了外部网格的所有可用空间,这就是您无法看到背景的原因。

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

    只有宽度是可以的,但高度呢。如果您只是将高度变大,那么您的子项就会显示出来。

    或者更好的是在内部子元素中留出边距。

    边距=“5”

    或让内部孩子像

    一样透明

    不透明度=“0.2”

    【讨论】:

    • 谢谢!我快疯了:)
    • 当我使用绝对路径在硬盘上的图像时,它们会自动导出吗?
    • 任何外部资源都不会被导出,因为它们不存在于内部对象树中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多