具有透明背景且包含不透明控件的WPF窗口 [英] WPF Window with transparent background containing opaque controls

查看:734
本文介绍了具有透明背景且包含不透明控件的WPF窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下外观的窗口:

I have a window with the following appearance:

但是我想要的是 Button 控件(灰色在窗口的主 Grid 的主窗口中,其不透明度为1,完全不透明。当我继承该项目时,在打开的 Window 标记内,顶层的不透明度设置为0.75。现在,据我所知,这将自动对所有子项强制执行,并且表示子项无法覆盖。

What I would like, however, is if the Button controls (the gray ones with text in the middle) in the Window's main Grid had an opacity of 1, totally opaque. As I inherited this project the opacity was set to 0.75 at the top level, inside the opening Window tag. Now as I understand this will automatically enforce that on all children and that said children cannot override.

那我该如何实现透明背景但不透明的按钮呢?到目前为止,我发现的唯一方法(作为WPF的相对新手)是拥有两个单独的Windows,一个是透明背景,另一个没有背景,但包含不透明的控件。

How then can I accomplish the transparent background but opaque buttons? The only way I have found so far (as a relative novice in WPF) is to have two separate Windows, one which is the transparent background, and the other has no background but contains the opaque controls. This is terribly hacky though and I want to avoid it if I can.

我可以根据要求提供代码,但实际上就像 window ,其windowstyle = none和不透明度.75包含 Grid ,其中包含一些非常基本的 Button 等控件。

I can supply code if requested, but it is really as simple as a Window with windowstyle=none and opacity .75 containing a Grid, which contains some very basic Button etc controls.

是否有人曾构建过这样的 Window ,否则是否有洞察力来生成一个?

Has anyone built such a Window before or otherwise have insight into generating one? Thanks.

推荐答案

不是设置窗口的不透明度,而是设置背景的不透明度:

Instead of setting the opacity of the window, set its background's opacity:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        AllowsTransparency="True" WindowStyle="None">
    <Window.Background>
        <SolidColorBrush Opacity="0.5" Color="White"/>
    </Window.Background>
    <Grid>
        <Button Width="200" Height="50">button</Button>
    </Grid>
</Window>

这篇关于具有透明背景且包含不透明控件的WPF窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆