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

后端 未结 3 1472
鱼传尺愫
鱼传尺愫 2020-12-08 09:54

When you set the opacity on a Grid in WPF, all the child elements appear to inherit its Opacity. How can you have a child element not inherit the p

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 10:42

    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. Something like this, where the 0C in the background is the Alpha channel (the AA in AARRGGBB):

    
    
    
    
    
    

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

提交回复
热议问题