【问题标题】:How do you override the opacity of a parent control in WPF?如何覆盖 WPF 中父控件的不透明度?
【发布时间】:2011-02-08 16:25:45
【问题描述】:

当您在 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>

【问题讨论】:

    标签: wpf transparency opacity


    【解决方案1】:

    您可以在布局网格内叠加两个网格。第一个将被定义为您的网格,减去最里面的红色网格。第二个将定义为具有相同的列和行,具有透明背景。此网格的唯一子元素将是您最内层的网格。

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

    【讨论】:

    • @Wonko,谢谢。这种方法是院长提供的答案的一个很好的替代方法。
    【解决方案2】:

    我能够在纯 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>
    

    【讨论】:

    • 谢谢大佬。纯色刷子成功了!欢迎来到 StackOverflow (_)/
    • 您是否有链接到实施此功能的站点?我正在尝试覆盖纯 html/css3 中的不透明度,并且真的很想看到这种方法的结果 html。
    • 太棒了!可悲的是 VS 没有内置方法来区分是否继承父级不透明度。这不仅仅适用于网格,我已经实现了这种技术来获得具有正常不透明内容的半透明 TabControl。
    【解决方案3】:

    如果您希望父容器的所有子容器都设置自己的不透明度,而不管父容器如何,您可以只设置父面板背景的 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 通道或不透明蒙版来做到这一点。如果不是,您可以构建某种自定义控件,为您提供所需的行为。我将不得不考虑一下,看看哪种方案可能是这种场景的最佳解决方案。

    【讨论】:

    • 目的是让除一个之外的所有孩子都遵守父母的不透明度。也许可以在本机 xaml 中设置一些东西而无需构建自定义控件?
    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2016-09-28
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2013-04-18
    相关资源
    最近更新 更多