WPF中透明中的不透明元素 [英] opaque element in a transparent in WPF

查看:72
本文介绍了WPF中透明中的不透明元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想要一个透明的窗口,但下面有完全不透明的子控件.但是,WPF 使所有子项都透明.

In my application I'd like to have a transparent window but fully opaque children controls underneath. However, WPF makes all children transparent.

请参阅下面的 XAML.正如预期的那样,网格是半透明的 50%,但其中的矩形是透明的而不是不透明的,即使认为 opacity="1".有没有办法实现这一目标?

See the XAML below. The grid is semi-transparent 50% as expected but the rectangle in it is trasparent not opaque even thought opacity="1". Is there any way to achieve this?

<Window x:Class="WpfApplication10.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" AllowsTransparency="True" Height="300" ResizeMode="NoResize" Width="300" WindowStyle="None" Background="Transparent"  >

    <Border   BorderBrush="black"  BorderThickness="7" CornerRadius="10">
        <Grid Background="Red" Opacity="0.5"     >

        <Rectangle Width="100" Height="100" Fill="white" Opacity="1"/>

    </Grid>
    </Border></Window>

谢谢,细胞

推荐答案

矩形不完全不透明的原因是因为您的容器(网格)的不透明度为 0.5,并且不透明度会被继承给子对象.

The reason why your rectangle is not fully opaque is because your container (the grid) has an opacity of .5, and opacity gets inherited to the child objects.

相反,尝试将网格的背景画笔更改为半透明,例如:

Instead, try changing the Background Brush of the Grid to something semi-transparent like:

<Grid Background="#66ff0000">

这应该会给你一个半透明的网格和一个完全不透明的矩形.

This should give you a semi-transparent Grid and a fully opaque rectangle.

这篇关于WPF中透明中的不透明元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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