2009-06-11 244 views
14

在我的應用程序中,我希望有一個透明的窗口,但下面是完全不透明的兒童控件。但是,WPF讓所有的孩子都透明。WPF中透明的不透明元素

請參閱下面的XAML。如預期的那樣網格是半透明的50%,但其中的矩形是透明的而不透明的,即使認爲不透明度=「1」。有什麼辦法可以做到這一點?

<Window x:Class="WpfApplication10.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" AllowsTransparency="True" Height="300" ResizeMode="NoResize" Width="300" WindowStyle="None" Background="Transparent" > 

    <Border BorderBrush="black" BorderThickness="7" CornerRadius="10"> 
     <Grid Background="Red" Opacity="0.5"  > 

     <Rectangle Width="100" Height="100" Fill="white" Opacity="1"/> 

    </Grid> 
    </Border></Window> 

感謝, cellik

回答

19

之所以您的矩形並不完全不透明是因爲你的容器(電網)具有.5不透明和不透明度被繼承到子對象。

相反,嘗試改變電網的背景刷的東西半透明狀:

<Grid Background="#66ff0000"> 

這應該給你一個半透明網格和完全不透明的矩形。

+0

謝謝,這個答案真的有用! – ariso 2009-10-29 13:48:14