如何在WPF中覆盖父控件的不透明度?

时间:2010-04-16 04:17:05

标签: wpf transparency opacity

在WPF中的Grid上设置不透明度时,所有子元素似乎都会继承其Opacity。如何让子元素不继承父元素的不透明度?

例如,以下父网格中间有一个子网格,背景设置为红色,但由于父级的不透明度,背景显示为粉红色。我希望子网格具有纯色,不透明的背景:

<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>

3 个答案:

答案 0 :(得分:44)

我能够使用Brush绘制主网格背景,在纯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>

答案 1 :(得分: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 -->

答案 2 :(得分:1)

如果您希望父容器的所有子容器都设置自己的不透明度而不管父项,您只需设置父面板背景的Alpha通道(而不是设置不透明度)即可获得略微透明的背景而不会影响孩子元素。像这样的东西,背景中的0C是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" />

但是,如果你想要除了一个孩子以外的所有孩子都坚持父母的不透明度,这有点复杂。您可以使用ControlTemplate和一些使用Alpha通道或不透明蒙板的巧妙技巧来做到这一点。如果没有,你可以构建某种自定义控件,为您提供所需的行为。我需要考虑一下,看看对于那种情况可能是最好的解决方案。