Assembly: PresentationCore (in presentationcore.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
/** @property */ public double get_Opacity () /** @property */ public void set_Opacity (double value)
Property ValueThe value of the Opacity property is expressed as a value between 0.0 and 1.0. The default value is 1.0.
Metadata properties set to true
A Opacity value of 0 indicates that the brush is completely transparent, while a value of 1 indicates that the brush is completely opaque. A value of 0.5 indicates the brush is 50% opaque, a value of 0.725 indicates the brush is 72.5% opaque, and so on. Values less than 0 are treated as 0, while values greater than 1 are treated as 1.
This example shows how to make a UIElement transparent or 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. A value of 0.5 makes the element 50% opaque, and so on. An element's Opacity is set to 1.0 by default.
The following example sets the Opacity of a button to 0.25, making it and its contents (in this case, the button's text) 25% opaque.
<!-- Both the button and its text are made 25% opaque. --> <Button Opacity="0.25">A Button</Button>
// // Both the button and its text are made 25% opaque. // Button myTwentyFivePercentOpaqueButton = new Button(); myTwentyFivePercentOpaqueButton.Opacity = new Double(); myTwentyFivePercentOpaqueButton.Opacity = 0.25; myTwentyFivePercentOpaqueButton.Content = "A Button";
If an element's contents have their own Opacity settings, those values are multiplied against the containing elements Opacity.
The following example sets a button's Opacity to 0.25, and the Opacity of an Image control contained with in the button to 0.5. As a result, the image appears 12.5% opaque: 0.25 * 0.5 = 0.125.
<!-- The image contained within this button has an effective opacity of 0.125 (0.25 * 0.5 = 0.125). --> <Button Opacity="0.25"> <StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Margin="10">A Button</TextBlock> <Image Source="sampleImages\berries.jpg" Width="50" Height="50" Opacity="0.5"/> </StackPanel> </Button>
// // The image contained within this button has an // effective opacity of 0.125 (0.25*0.5 = 0.125); // Button myImageButton = new Button(); myImageButton.Opacity = new Double(); myImageButton.Opacity = 0.25; StackPanel myImageStackPanel = new StackPanel(); myImageStackPanel.Orientation = Orientation.Horizontal; TextBlock myTextBlock = new TextBlock(); myTextBlock.VerticalAlignment = VerticalAlignment.Center; myTextBlock.Margin = new Thickness(10); myTextBlock.Text = "A Button"; myImageStackPanel.Children.Add(myTextBlock); Image myImage = new Image(); BitmapImage myBitmapImage = new BitmapImage(); myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri("sampleImages/berries.jpg",UriKind.Relative); myBitmapImage.EndInit(); myImage.Source = myBitmapImage; ImageBrush myImageBrush = new ImageBrush(myBitmapImage); myImage.Width = 50; myImage.Height = 50; myImage.Opacity = 0.5; myImageStackPanel.Children.Add(myImage); myImageButton.Content = myImageStackPanel;
Another way to control the opacity of an element is to set the opacity of the Brush that paints the element. This approach enables you to selectively alter the opacity of portions of an element, and provides performance benefits over using the element's Opacity property. The following example sets the Opacity of a SolidColorBrush used to paint the button's Background is set to 0.25. As a result, the brush's background is 25% opaque, but its contents (the button's text) remain 100% opaque.
<!-- This button's background is made 25% opaque, but its text remains 100% opaque. --> <Button> <Button.Background> <SolidColorBrush Color="Gray" Opacity="0.25" /> </Button.Background> A Button </Button>
// // This button's background is made 25% opaque, // but its text remains 100% opaque. // Button myOpaqueTextButton = new Button(); SolidColorBrush mySolidColorBrush = new SolidColorBrush(Colors.Gray); mySolidColorBrush.Opacity = 0.25; myOpaqueTextButton.Background = mySolidColorBrush; myOpaqueTextButton.Content = "A Button";
You may also control the opacity of individual colors within a brush. For more information about colors and brushes, see Painting with WPF Brushes. For an example showing how to animate an element's opacity, see Animate the Opacity of an Element or Brush.
|How to: Animate the Opacity of an Element or Brush|| |
To make a framework element fade in and out of view, you can animate its Opacity property or you can animate the Opacity property of the Brush (or brushes) used to paint it. Animating the element's opacity makes it and its children fade in and out of view, but animating the brush used to paint the element enables you to be more selective about which portion of the element fades. For example, you could animate the opacity of a brush used to paint a button's background. This would cause the button's background to fade in and out of view, while leaving its text fully opaque.
|How to: Animate the Color or Opacity of a SolidColorBrush|
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.