轉自:http://hi.baidu.com/willbelate/item/8296f40b15a7e3354bc4a35e

<Window x:Class="WPF中的三種透明方法.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <Grid.Background> <ImageBrush ImageSource="D:\c.png" Stretch="Fill" ViewportUnits="RelativeToBoundingBox" Viewport="0,0,0.1,0.1" TileMode="Tile"/> </Grid.Background> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Button Grid.Row="0" Content="Hello" Opacity="0.5"></Button> <Button Grid.Row="1" Content="Hello" Background="#90000000"></Button> <Button Grid.Row="2" Content="Hello"> <Button.OpacityMask> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Transparent" Offset="0.3"/> <GradientStop Color="Blue" Offset="1"/> </LinearGradientBrush> </Button.OpacityMask> </Button> </Grid> </Window>
在WPF中可以使用三種方法使元素透明,
第一種方法是設置元素的Opacity屬性,如果元素不透明,則Opacity的值為1,如果透明則值為大於0小於1的小數數值。
第二種方法是通過元素的背景顏色,可以使用#90000000的方式設置,也就是Argb的方式設置,#號后面的兩位置透明顏色,如果出現#FF000000那么背景色是全透明的。
第三種方法是通過元素的OpacityMask透明掩碼屬性來設置透明,此屬性接收一個畫刷對象,可以使用LinearGradientBrush制做線性漸變透明的效果,也可以使用SolidBrush制做普通透明的畫刷。
比較快的方法是Opacity="x"(0<x<1)