简体   繁体   English

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

[英]How do you override the opacity of a parent control in WPF?

When you set the opacity on a Grid in WPF, all the child elements appear to inherit its Opacity . 在WPF中的Grid上设置不透明度时,所有子元素似乎都会继承其Opacity How can you have a child element not inherit the parent's opacity? 如何让子元素不继承父元素的不透明度?

For example, the following parent grid has one child grid in the middle with a background set to red, but the background appears pinkish because of the parent's opacity. 例如,以下父网格中间有一个子网格,背景设置为红色,但由于父级的不透明度,背景显示为粉红色。 I'd like the child grid to have a solid color, non-transparent background: 我希望子网格具有纯色,不透明的背景:

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

I was able to achieve something like this in pure xaml using a Brush to paint the main grids background. 我能够使用Brush绘制主网格背景,在纯xaml中实现这样的效果。 This way only parent grid will have its opacity set and its child elements won't inherit it. 这样,只有父网格将设置其不透明度,其子元素将不会继承它。

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

You can just overlay two grids inside your layout grid. 您可以在布局网格中叠加两个网格。 The first would be defined as your grid, minus your red innermost grid. 第一个将被定义为您的网格,减去您的红色最内层网格。 The second would be defined with the same columns and rows, with a transparent background. 第二个将使用相同的列和行定义,具有透明背景。 The only child of this grid would be your innermost grid. 这个网格中唯一的孩子就是你最里面的网格。

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

If you want all the children of the parent container to set their own opacity regardless of the parents you can just set the alpha channel of the parent panel's background (instead of setting opacity) to get a slightly transparent background without impacting the child elements. 如果您希望父容器的所有子容器都设置自己的不透明度而不管父项,您只需设置父面板背景的Alpha通道(而不是设置不透明度)以获得稍微透明的背景而不会影响子元素。 Something like this, where the 0C in the background is the Alpha channel (the AA in AARRGGBB): 像这样,背景中的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" />

However, if you want all the children except one to adhere to the parent's opacity that is a little more complicated. 但是,如果你想要除了一个孩子以外的所有孩子都坚持父母的不透明度,这有点复杂。 You might be able to do that with a ControlTemplate and some clever tricks with the Alpha channels or an opacity mask. 您可以使用ControlTemplate和一些使用Alpha通道或不透明蒙板的巧妙技巧来做到这一点。 If not you could build some sort of custom control that gave you the behavior you wanted. 如果没有,你可以构建某种自定义控件,为您提供所需的行为。 I would have to think about it for a bit to see what might be the best solution for that type of scenario. 我需要考虑一下,看看这种情况下最好的解决方案是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于父控件的不透明度控制后代控件的不透明度? - how to control opacity of descendant controls based on parent control's opacity ? 在WPF应用程序中,如何覆盖“控制台关闭”命令? - In a WPF app how do you override the Console Close command? WPF自定义控件 - 如何对自定义控件进行单元测试? - WPF Custom Control - How do you unit test a custom control? 怎么用WPF中的Frame控件做过渡效果? - How do you do transition effects using the Frame control in WPF? 如何自定义WPF TextBox控件(模板)外观? - How do you customize a WPF TextBox control (template) appearance? 如何在WPF中将控件的DataContext绑定到其内容的DataContext? - How do you bind the DataContext of a Control to the DataContext of its Content in WPF? 您如何在WPF日历控件上检测到自动的Month转换? - How do you detect an automatic Month shift on the WPF Calendar control? 如果父元素位于WPF的另一个文件中,如何绑定它们? - How do you bind to parent elements if they are in another file in WPF? 如何覆盖不在(xaml / wpf)中的网格上的控件 - How do you overlay a control over a grid that it is not in (xaml/wpf) 你怎么知道你的WPF控件是否被渲染? - How do you know if your WPF control is being rendered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM