简体   繁体   English

如何在WPF中使不透明度变深

[英]How to make opacity darker in wpf

当用户单击某个按钮时,我的主窗口背景颜色为Background="#005075" ,弹出一些小窗口,然后我改变主窗口的不透明度,如Opacity="0.5" ,问题在于它使背景打火机,我该如何在WPF中暗中摆弄?

I do something similar a lot. 我做了很多类似的事情。 The best way to do this is by having a couple of top-level grid's in your Window and work with those rather than the Window itself. 最好的方法是在Window中有几个顶级网格并与之一起使用,而不是Window本身。

For example, Ill do something like this: 例如,我会做这样的事情:

<Window x:Class="WpfApp9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp9"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <!-- Background grid with 100% opacity -->
        <Grid Background="#005075"/>
        <!-- main grid, which sits on top of the background grid -->
        <Grid x:Name="MainGrid" Background="Transparent" Opacity="0.5">
            <!-- Windows controls go here -->
        </Grid>
    </Grid>
</Window>

Opacity will not make it darker. 不透明度不会使它变暗。 What you can do is you can make some kind of panel which is stretched all over your main window, and has a black color and opacity of 0.2 or something like that, and mainly is hidden by default. 您可以做的是,可以制作某种面板,该面板在整个主窗口中拉伸,并且具有黑色和0.2或类似的不透明度,并且默认情况下主要是隐藏的。 And when you want to make the main window disabled you make that panel visible. 而当您要禁用主窗口时,则使该面板可见。 This should solve your problem. 这应该可以解决您的问题。

You can also use it as a loading panel, if you add some animation in center or something like this. 如果您在中间添加了一些动画或类似的东西,您也可以将其用作加载面板。 So it will be over all of the content of your main window and will not allow the user to interact with your main window while it's disabled. 因此,它将覆盖您主窗口的所有内容,并且不允许用户在禁用时与您的主窗口进行交互。

PS The black panel with opacity 0.2 should be positioned over all of the controls in main window PS不透明度为0.2的黑色面板应放置在主窗口中的所有控件上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM