2010-04-16 93 views
26

當您將不透明度設置爲WPF中的Grid時,所有子元素都顯示爲繼承其Opacity。你怎麼能有一個子元素不繼承父母的不透明?如何覆蓋WPF中父控件的不透明度?

例如,以下父網格中間有一個子網格,背景設置爲紅色,但由於父級的不透明度,背景顯得帶粉紅色。我想孩子電網有一個堅實的顏色,不透明的背景:

<Grid x:Name="LayoutRoot"> 

    <Grid Background="Black" Opacity="0.5"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="0.333*"/> 
     <RowDefinition Height="0.333*"/> 
     <RowDefinition Height="0.333*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="0.333*"/> 
     <ColumnDefinition Width="0.333*"/> 
     <ColumnDefinition Width="0.333*"/> 
    </Grid.ColumnDefinitions> 

    <-- how do you make this child grid's background solid red 
     and not inherit the Opacity/Transparency of the parent grid? --> 
    <Grid Grid.Column="1" Grid.Row="1" Background="Red"/> 
    </Grid> 

</Grid> 

回答

37

我能夠在純xaml中使用畫筆繪製主網格背景來實現這樣的效果。 這種方式只有父網格將設置其不透明度,其子元素不會繼承它。

<Grid x:Name="LayoutRoot">  
     <Grid> 
     <Grid.Background> 
      <SolidColorBrush Color="Black" Opacity="0.5"/> 
     </Grid.Background> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="0.333*"/> 
      <RowDefinition Height="0.333*"/> 
      <RowDefinition Height="0.333*"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="0.333*"/> 
      <ColumnDefinition Width="0.333*"/> 
      <ColumnDefinition Width="0.333*"/> 
     </Grid.ColumnDefinitions> 

     <Grid Grid.Column="1" Grid.Row="1" Background="Red" /> 
     </Grid> 
</Grid> 
+0

。純色筆刷的竅門!並歡迎來到StackOverflow \(* _ *)/ – 2010-04-16 18:49:09

+0

您是否有鏈接到實施此網站的網站?我試圖在純html/css3中覆蓋不透明,並且真的很想看到這種方法的結果html。 – 2012-02-07 02:02:46

+1

太棒了!很遺憾,VS沒有內置的方法來區分繼承父類的不透明性。這適用於多個網格,我已經實現了這種技術,以獲得具有正常不透明度內容的半透明TabControl。 – 2014-01-07 17:01:33

1

如果你想在父容器的所有孩子給自己設定的不透明度不顧父母的,你可以只設置alpha通道的父面板的背景(而不是設置不透明度)以獲得稍微透明的背景,而不會影響子元素。事情是這樣的,在0℃的背景是Alpha通道(在AARRGGBB的AA):

<Grid Grid.Column="0" 
     Grid.Row="1" 
     Background="Red" 
     Opacity="1" /> 

<Grid Grid.Column="1" 
     Grid.Row="1" 
     Background="Green" /> 

<Grid Grid.Column="2" 
     Grid.Row="1" 
     Background="Blue" /> 

但是,如果希望除了一個孩子以外的所有孩子都堅持父母的不透明度,這是一個更加comp licated。您可以通過ControlTemplate和Alpha通道或不透明蒙版的一些聰明技巧來做到這一點。如果沒有,你可以建立一些自定義控件,給你你想要的行爲。我不得不考慮一下,看看什麼可能是這種情況下的最佳解決方案。

+0

意圖是讓所有的孩子除了一個堅持父母的不透明。也許有沒有建立自定義控件可以在本機xaml中設置的東西?感謝首席 – 2010-04-16 14:17:54

3

您可以在佈局網格中疊加兩個網格。第一個將被定義爲你的網格,減去你的紅色最內層網格。第二個將被定義爲具有相同的列和行,並具有透明背景。這個網格的唯一孩子將是你最內層的網格。

<Grid x:Name="LayoutRootNew" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch"> 

     <Grid Background="Black" Opacity="0.5"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="0.333*"/> 
       <RowDefinition Height="0.333*"/> 
       <RowDefinition Height="0.333*"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="0.333*"/> 
       <ColumnDefinition Width="0.333*"/> 
       <ColumnDefinition Width="0.333*"/> 
      </Grid.ColumnDefinitions> 

      <TextBlock Grid.Column="0" Grid.Row="0"> 
       Here is some content in a somewhat transparent cell 
      </TextBlock> 

     </Grid> <!-- End of First Grid --> 

     <!-- Second grid --> 
     <Grid Background="Transparent"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="0.333*"/> 
       <RowDefinition Height="0.333*"/> 
       <RowDefinition Height="0.333*"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="0.333*"/> 
       <ColumnDefinition Width="0.333*"/> 
       <ColumnDefinition Width="0.333*"/> 
      </Grid.ColumnDefinitions> 

      <Grid Grid.Column="1" Grid.Row="1" Background="Red"> 
       <TextBlock Foreground="White" Text="Here Is Your Red Child" /> 
      </Grid> <!-- Inner Child Grid --> 
     </Grid> <!-- End of Second Grid --> 
    </Grid>  <!-- Layout Grid --> 
+0

@Wonko,謝謝。這種方法是首席答案提供的一個很好的選擇。 – 2010-04-16 18:50:29