Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

like image 814
NoizWaves Avatar asked Mar 08 '09 14:03

NoizWaves


People also ask

How do I make the background transparent in WPF?

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

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.

What is opacity in XAML?

Opacity in XAML is defined as a double, not an HTML color triplet. http://msdn.microsoft.com/en-us/library/system.windows.uielement.opacity.aspx. You'll want to set it like this: <TextBlock Opacity="0" /> You can also use a brush to set it: <SolidColorBrush Color="#FF295564" Opacity="0.3"/>


1 Answers

You could set IsHitTestVisible to False on your masking element.

<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.

like image 94
Drew Noakes Avatar answered Oct 04 '22 05:10

Drew Noakes