Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

.net - Change Background opacity without changing content opacity

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...