改变背景不透明度,不改变内容不透明度

时间:2021-05-16 19:56:01

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窗口背景的不透明度。当我将窗口属性“不透明度”更改为0.5时,我确实得到了一个半透明的窗口,但是窗口内的图像也继承了0.5的不透明度值,所以我怎么能只对窗口进行不透明度呢?

1 个解决方案

#1


31  

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 Opacity of the Window.Background.

窗口是所有内容的父容器,所以设置窗口的不透明度将会影响它包含的所有内容。我认为你要做的是改变Window.Background的不透明度。

Enabling a window to do transparency involves a couple things to be added. First, you will need to set Window.AllowsTransparency = True and also set the Window.WindowStyle = None. WindowStyle.None creates 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.Background to have a brush with an Opacity set on it.

要使窗口实现透明,需要添加一些东西。首先,需要设置窗口。允许值= True,并设置窗口。WindowStyle =没有。WindowStyle。没有一个窗口没有最小化、最大化和关闭窗口的按钮,所以您必须在应用程序中自己处理,同时调整窗口大小和移动窗口。做完这些之后,你就可以设置窗口了。设置不透明度的画笔。

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.

如果只希望窗口中的元素可见,则可以将窗口背景设置为透明。

#1


31  

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 Opacity of the Window.Background.

窗口是所有内容的父容器,所以设置窗口的不透明度将会影响它包含的所有内容。我认为你要做的是改变Window.Background的不透明度。

Enabling a window to do transparency involves a couple things to be added. First, you will need to set Window.AllowsTransparency = True and also set the Window.WindowStyle = None. WindowStyle.None creates 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.Background to have a brush with an Opacity set on it.

要使窗口实现透明,需要添加一些东西。首先,需要设置窗口。允许值= True,并设置窗口。WindowStyle =没有。WindowStyle。没有一个窗口没有最小化、最大化和关闭窗口的按钮,所以您必须在应用程序中自己处理,同时调整窗口大小和移动窗口。做完这些之后,你就可以设置窗口了。设置不透明度的画笔。

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.

如果只希望窗口中的元素可见,则可以将窗口背景设置为透明。