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
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.
WPF control's background can be transparent by setting the Background property to null.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With