【问题标题】:combine image and solidcolor background for a WPF Form为 WPF 表单组合图像和纯色背景
【发布时间】:2009-02-04 19:19:46
【问题描述】:

我正在构建一个 WPF 表单。我想为窗口指定一个背景图像,这很容易。但是,我还想指定一种颜色,以便图像未覆盖的表单区域为白色。我已经看到一些示例显示使用两种不同的背景画笔,但是当我尝试 VS.NET 时告诉我我不能有多个画笔。

这是我正在使用的 XAML

<Window x:Class="Consent.Client.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="http://www.codeplex.com/CompositeWPF"
    Title="Shell" WindowStyle="None" WindowState="Maximized" FontSize="24">
    <Window.Background>
        <ImageBrush AlignmentX="Left" AlignmentY="Top"  Stretch="None" TileMode="None" ImageSource="logo_header2.png" />
    </Window.Background>
    <ItemsControl Background="White" VerticalAlignment="Center" cal:RegionManager.RegionName="MainRegion" >
    </ItemsControl>
</Window>

这对图像很有效,但图像未覆盖的背景是黑色的。我怎样才能让它变白?更改图像本身并不是一个真正的选择。

【问题讨论】:

    标签: wpf background


    【解决方案1】:

    试试这个(我删除了与问题不直接相关的所有内容以使代码更清晰):

    <Window x:Class="Consent.Client.Shell"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Background="White">
       <Grid>
          <Grid.Background>
             <ImageBrush ImageSource="logo_header2.png" />
          </Grid.Background>
          <ItemsControl>
          </ItemsControl>
       </Grid>
    </Window>
    

    基本上,将窗口的背景设置为图像背后的颜色,而不是在窗口中放置一个网格并为网格提供背景图像,将所有内容放在网格内而不是直接放在窗口中。

    【讨论】:

      【解决方案2】:

      作为Nirs 答案的扩展。如果您想在内容周围留出边距,但让背景图像能够填满整个窗口,您还可以使用边框堆叠背景:

      <Window x:Class="Consent.Client.Shell"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Background="White">
      
          <Border Padding="10">
              <Border.Background>
                  <ImageBrush ImageSource="logo_header2.png" />
              </Border.Background>
           <!--<Your content >-->
          </Border>
      
      </Window>
      

      【讨论】:

        【解决方案3】:

        我不确定你是否可以组合画笔。您可以使用 ImageBrush,或者您可以忘记“背景”并将项目堆叠在一个网格中:

        <Window x:Class="Consent.Client.Shell"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:cal="http://www.codeplex.com/CompositeWPF"
            Title="Shell" WindowStyle="None" WindowState="Maximized" FontSize="24">
            <Grid>
                <Image Source="logo_header2.png" Stretch="None" VerticalAlignment="Top" />
                <ItemsControl Background="White" VerticalAlignment="Center" cal:RegionManager.RegionName="MainRegion" >
                </ItemsControl>
            </Grid>
        </Window>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-18
          • 1970-01-01
          • 2011-07-26
          • 2011-01-10
          • 2020-09-25
          • 1970-01-01
          相关资源
          最近更新 更多