我有一个正在构建的 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>
这对图像很有用,但图像未覆盖的背景是黑色的。怎么让它变白?改变图像本身并不是一个真正的选择。
最佳答案
试试这个(我删除了与问题不直接相关的所有内容以使代码更清晰):
<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>
基本上,将窗口的背景设置为图像颜色的后面,而不是在窗口中放置一个网格并为网格提供背景图像,将所有内容放在网格内而不是直接在窗口中。
关于wpf - 为 WPF 表单组合图像和纯色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/512869/