Condividi tramite


Opacity (UIElement)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the degree of opacity of an object.

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

Property Value

Type: Double

A value between 0 and 1.0 that specifies the opacity of the object. 1.0 means full opacity and 0 means transparent.

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

Managed Equivalent

Opacity

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. To exclude an element from hit-testing, set the IsHitTestVisible property to false.

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

Opacity values that are greater than 1.0 are treated as 1.0 when the value is used. For example, setting an Opacity to 2 to cancel out the effects of a nested element with 0.5 Opacity does not work; the 2 value is treated as 1.0 even before the nested element factoring is calculated. However, getting the property value will return the original value of 2. Opacity values that are less than zero are treated as 0 when the value is used.

NoteNote:

For best performance, use the Visibility property instead of the Opacity property whenever possible. The Visibility property is best when you want to make an object completely visible or completely invisible, instead of 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

Border (Silverlight 2)

Canvas

Ellipse

Glyphs

Image

InkPresenter

Line

MediaElement

PasswordBox (Silverlight 2)

Path

Polygon

Polyline

Popup (Silverlight 2)

Rectangle

StackPanel (Silverlight 2)

TextBlock

TextBox (Silverlight 2)

See Also

Reference