C# 如何在 WPF 中的元素上添加半透明阴影?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/623661/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:43:37  来源:igfitidea点击:

How to add a semi-transparent shade over elements in WPF?

c#.netwpfuser-interface

提问by NoizWaves

I would like to add a semi-transparent colour over the contents of a WPF window (to indicate the state of the window). Currently I am using a UserControl that fills the Window, and I change the Background colour and Visibility as required.

我想在 WPF 窗口的内容上添加半透明颜色(以指示窗口的状态)。目前我正在使用填充窗口的 UserControl,并根据需要更改背景颜色和可见性。

The problem with this method is when the UserControl is visible, I cannot click any controls (Buttons, CheckBoxes) in the Window behind the UserControl. I guess I need to make the UserControl transparent to clicks somehow. Is this possible, or is there a better way to add the colour over the Window?

此方法的问题是当 UserControl 可见时,我无法单击 UserControl 后面的窗口中的任何控件(按钮、复选框)。我想我需要以某种方式使 UserControl 对点击透明。这是可能的,还是有更好的方法在窗口上添加颜色?

采纳答案by Drew Noakes

You could set IsHitTestVisibleto Falseon your masking element.

您可以在掩蔽元素上设置IsHitTestVisibleFalse

<Grid>
   <Button>Background Button</Button>
   <Rectangle Fill="Blue" Opacity="0.25" IsHitTestVisible="False"/>
</Grid>

Try that XAML out in something like Kaxaml. You will still be able to click the button, yet the blue rectangle will be presented over the top. It is semi-transparent due to the low opacity setting.

Kaxaml 之类的东西中尝试使用 XAML 。您仍然可以单击该按钮,但蓝色矩形将显示在顶部。由于低不透明度设置,它是半透明的。

回答by orcun

There is an IsHitTestVisible property.

有一个 IsHitTestVisible 属性。