WPF支持真正的透明效果。這意味着,若是在一個性質或元素上層疊另外幾個形狀或元素,並讓全部這些形狀和元素具備不一樣的透明度,就會看到所指望的效果。經過該特性可以建立透過上面的元素能夠看到的的圖像背景,這是最簡單的情形。最複雜的情形是,使用該特性可建立多層動畫和其餘效果,對於其餘框架來講這是很難實現的。html
1、使用元素半透明app
可採用如下幾種方法使元素具備半透明效果:框架
下圖顯示的例子具備多個半透明層。性能
下面是XAML中的窗口內容:動畫
<Window x:Class="Drawing.Transparency" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="White" Title="Transparency" Height="385" Width="450"> <StackPanel Margin="5"> <StackPanel.Background> <ImageBrush ImageSource="celestial.jpg" Opacity="0.7"/> </StackPanel.Background> <Button Foreground="White" FontSize="16" Margin="10" BorderBrush="White" Background="#60AA4030" Padding="20">A Semi-Transparent Button</Button> <Label Margin="10" FontSize="18" FontWeight="Bold" Foreground="White">Some Label Text</Label> <TextBox Margin="10" Background="#AAAAAAAA" Foreground="White" BorderBrush="White">A semi-transparent text box</TextBox> <Button Margin="10" Padding="25" BorderBrush="White" > <Button.Background> <ImageBrush ImageSource="happyface.jpg" Opacity="0.6" TileMode="Tile" Viewport="0,0,0.1,0.4"/> </Button.Background> <StackPanel> <TextBlock Foreground="#75FFFFFF" TextAlignment="Center" FontSize="30" FontWeight="Bold" TextWrapping="Wrap" >Semi-Transparent Layers</TextBlock> </StackPanel> </Button> </StackPanel> </Window>
透明是較受歡迎的WPF特性之一。實際上,透明特性很是容易使用並且工做的很是好,全部有些過於氾濫地被用於WPF用戶界面。所以主義不要過分使用透明特性。spa
2、透明掩碼code
Opacity屬性使用元素的全部內容都是部分透明的。OpacityMask屬性提供了更大的靈活性。可以使用元素的特定區域透明或部分透明,從而實現各類常見的以及新穎的效果。例如,可以使用OpacityMask屬性將形狀逐漸褪色到徹底透明。orm
OpacityMask屬性接受任何畫刷。畫刷的alpha一般肯定了什麼地方是透明的。例如,若是使用SolidColorBrush畫刷設置非透明顏色,元素將保持徹底可見。顏色的其餘細節(紅、綠和藍成分)並不重要,當設置OpacityMask屬性時會忽略它們。xml
使用SolidColorBrush畫刷設置OpacityMask屬性沒有什麼意義,由於可以使用Opacity屬性更容易地實現相同的效果。然而,當使用更特殊的畫刷類型時,例如使用LinearGradient或RadialGradientBrush畫刷,OpacityMask屬性就變得更有用了。使用漸變將一種純色變換到透明色,可建立在整個元素表面褪色的透明效果。例如,下面的按鈕就使用了這種效果:htm
<Window x:Class="Drawing.OpacityMask" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="OpacityMask" Height="300" Width="300"> <Window.Background> <ImageBrush ImageSource="grandpiano.jpg"></ImageBrush> </Window.Background> <Grid Margin="10,50"> <Button Background="Purple" FontSize="14" FontWeight="Bold"> <Button.OpacityMask> <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"> <GradientStop Offset="0" Color="Black"></GradientStop> <GradientStop Offset="1" Color="Transparent"></GradientStop> </LinearGradientBrush> </Button.OpacityMask> <Button.Content>A Partially Transparent Button</Button.Content> </Button> </Grid> </Window>
下圖在一個窗口上顯示了該按鈕,在該窗口中還顯示了一幅名貴鋼琴的圖片。
還可結合使用OpacityMask屬性和VisualBrush畫刷來建立反射效果。例如,如下標記建立了最多見的WPF效果之一——具備鏡像文本的文本框。當輸入文本時,VisualBrush畫刷就會在下面繪製反射文本。使用VisualBrush畫刷繪製一個矩形,該矩形使用OpacityMask屬性褪色反射的文本,使反射文本與上面真實的元素區別開來:
<Window x:Class="Drawing.Reflection" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Reflection" Height="208.8" Width="491.2" Background="LightSteelBlue" > <Grid Margin="10" Grid.IsSharedSizeScope="True" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="Auto" SharedSizeGroup="Row"></RowDefinition> <RowDefinition SharedSizeGroup="Row"></RowDefinition> </Grid.RowDefinitions> <TextBox Name="txt" FontSize="30">Here is some reflected text</TextBox> <Rectangle Grid.Row="1" RenderTransformOrigin="1,0.5"> <Rectangle.Fill> <VisualBrush Visual="{Binding ElementName=txt}"></VisualBrush> </Rectangle.Fill> <Rectangle.OpacityMask> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Offset="0.3" Color="Transparent"></GradientStop> <GradientStop Offset="1" Color="#44000000"></GradientStop> </LinearGradientBrush> </Rectangle.OpacityMask> <Rectangle.RenderTransform> <ScaleTransform ScaleY="-1"></ScaleTransform> </Rectangle.RenderTransform> </Rectangle> </Grid> </Window>
該例使用LinearGradientBrush畫刷在徹底透明的顏色和半透明的顏色之間進行漸變,是反射內容更加平淡。該例還使用RenderTransform翻轉矩形,是反射的內容上下顛倒。由於使用了該變換,因此漸變過渡點(gradient Stops)必須反向設置。下圖顯示了最終效果:
原文出處:https://www.cnblogs.com/Peter-Luo/p/12312764.html