Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opaque element in a transparent in WPF

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

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>

thanks, cellik

like image 504
cellik Avatar asked Jun 11 '09 16:06

cellik


People also ask

What is Opacity WPF?

The Opacity property of UIElement is used to set transparency of a control. The value of Opacity falls between 0.0 and 1.0 where 0.0 is fully transparent and 1.0 is fully opaque.

How do I make the background transparent in WPF?

WPF control's background can be transparent by setting the Background property to null.

What is Opacity in XAML?

All elements in XAML including all brushes have an Opacity attribute that defines the transparency of an element. The value of Opacity is between 0 and 1. The value 0 means an element is fully-transparent and value 1 means an element is fully opaque. The default value of Opacity is 1.

Which property you can use to make the control semi transparent?

To make an element transparent or semi-transparent, you set its Opacity property. A value of 0.0 makes the element completely transparent, while a value of 1.0 makes the element completely opaque.


1 Answers

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.

like image 155
micahtan Avatar answered Oct 25 '22 12:10

micahtan