wpf 更改背景不透明度而不更改内容不透明度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12646906/
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
Change Background opacity without changing content opacity
提问by Idan Shechter
I wanted to know how can I change the opacity of the background of the WPF Window without effecting the inner child controls. When I change the Window property 'Opacity' to 0.5 I do get a semi-transparent window, but the image inside the window also inherited the 0.5 opacity value, so how can I just make the opacity for the window only?
我想知道如何在不影响内部子控件的情况下更改 WPF 窗口背景的不透明度。当我将 Window 属性 'Opacity' 更改为 0.5 时,我确实得到了一个半透明窗口,但是窗口内的图像也继承了 0.5 的不透明度值,那么我怎样才能只为窗口设置不透明度呢?
回答by evanb
The window is the parent container of everything so setting the opacity on the window will effect everything that it contains. I think what you want to do is change the Opacityof the Window.Background.
窗口是所有内容的父容器,因此在窗口上设置不透明度将影响它包含的所有内容。我想你想要做的是改变Opacity的Window.Background。
Enabling a window to do transparency involves a couple things to be added. First, you will need to set Window.AllowsTransparency = Trueand also set the Window.WindowStyle = None. WindowStyle.Nonecreates a window without the minimize, maximize, and close buttons in the window chrome so you'll have to handle that in the application yourself along with resizing and moving the window. After that's all done, then you can set the Window.Backgroundto have a brush with an Opacityset on it.
使窗口具有透明度需要添加一些东西。首先,您需要设置Window.AllowsTransparency = True并设置Window.WindowStyle = None. WindowStyle.None在窗口镶边中创建一个没有最小化、最大化和关闭按钮的窗口,因此您必须自己在应用程序中处理该问题,同时调整窗口大小和移动窗口。全部完成后,您可以将其设置为Window.Background带有Opacity设置的画笔。
The following code sample will show you how to have the window always transparent and set the opacity of the window background to have a different opacity.
下面的代码示例将向您展示如何让窗口始终透明并将窗口背景的不透明度设置为不同的不透明度。
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
WindowStyle="None"
AllowsTransparency="True">
<Window.Background>
<SolidColorBrush Color="White" Opacity="0.5"/>
</Window.Background>
<Grid>
<!--Window Content-->
</Grid>
</Window>
You can always set the window background to transparent if you only want the elements in the window to be visible.
如果您只希望窗口中的元素可见,您始终可以将窗口背景设置为透明。

