Transparent Brushes in WPF

Transparent Brushes

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.

The following code snippet creates a SolidColorBrush with 50% transparency. You can set the Opacity attribute of any brush using same code. 

  1. <SolidColorBrush Color="Blue" Opacity="0.5"/>     
Now if we set Opacity of code in 25 as following, the output that looks like Figure 33 will look like Figure 34. 
  1. <VisualBrush Viewport="0,0,0.25, 0.25" TileMode="Tile" Opacity="0.5">  
The following code snippet sets the Opacity property of a VisualBrush at run-time.
  1. VisualBrush vBrush = new VisualBrush();  
  2.   
  3. vBrush.Opacity = 0.50; 

tb.jpg

Figure 34. A rectangle with a transparent Visual brush in Tile mode

Transparent Controls

Good part is, this Opacity property not only is available for brushes but also for all elements in WPF and XAML. That means, you can create transparent Windows, Pages, Controls, or User Controls by simply setting this property.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.