Opacity Property

Gets or sets the degree of the object's opacity.

XAML
<object Opacity="Double" .../>
Scripting
value = object.Opacity
object.Opacity = value

Property Value

Double

A value between 0 and 1.0 that declares the opacity, with 1.0 meaning full opacity and 0 meaning transparent.

This property is read/write. The default value is 1.0.

Remarks

An Opacity value of 0 does not exclude an element from hit testing. This behavior can be useful for creating imagemap-style overlays of Canvas, Rectangle or other shapes to capture hits on elements that exist at z-orders greater than other visible objects underneath. In order to exclude an element from hit testing, you should set IsHitTestVisible to true.

When Opacity is set on elements that are nested, the effective opacity is the product of all applicable opacity factors. For instance, if an element that has Opacity=0.5 is contained in a Canvas that is also Opacity=0.5, the effective Opacity value is 0.25.

Opacity values greater than 1.0 are treated as 1.0 when the value is used, although obtaining the property value will still give you the original greater-than-one value. Opacity values less than zero are treated as 0 when the value is used. For instance, setting an Opacity to 2 to cancel out the effects of a nested element with 0.5 Opacity element does not work; the 2 value is treated as 1.0 even before the nested-element factoring is calculated.

The following illustration shows a TextBlock displayed as a window-less plug-in on a Microsoft Virtual Earth mashup. The Background value of the plug-in is #00000000, which is totally transparent. In addition, the Opacity of the TextBlock is set to 0.7, which allows the map underneath to partially show through the text.

TextBlock with partial opacity

TextBlock with partial opacity

Performance Note   For best performance, use the Visibility property instead of the Opacity property when possible. The Visibility property is best when you simply want to make an object completely visible or completely not visible rather than using partial opacity or fading in or out of view. An object with an Opacity of 0 is still hit tested and the rendering engine still technically renders the object, you just cannot see it. Setting Visibility to Collapsed avoids these costs.

Applies To

Canvas, Ellipse, Glyphs, Image, InkPresenter, Line, MediaElement, Path, Polygon, Polyline, Rectangle, TextBlock

See Also

Object Positioning in Silverlight
Windowless