wpf 设置堆栈面板不透明度而不影响子控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17993318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:20:23  来源:igfitidea点击:

Set stackpanel opacity without affecting on child control

wpf

提问by jadav suresh

i am new in WPF i have stack panel and in this stack panel we added Text Block at code behind and also set stack panel background and Text Block foreground color at code behind. i have also set opacity dynamicaly when i set opacity of stackpanel than it affect on it's child control (i.e Textblock)

我是 WPF 的新手,我有堆栈面板,在此堆栈面板中,我们在代码后面添加了文本块,并在代码后面设置了堆栈面板背景和文本块前景色。当我设置堆栈面板的不透明度时,我还动态设置了不透明度,而不是影响它的子控件(即文本块)

Please give me proper solution for this.

请给我适当的解决方案。

Thank you.

谢谢你。

回答by Sonhja

If you want to do it in Xaml, you can create a SolidColorBrushat your Window.Resourcesto be the background color of your panel:

如果您想在 中执行此操作Xaml,您可以SolidColorBrush在您Window.Resources的面板中创建一个作为您面板的背景颜色:

<Window.Resources>
    <SolidColorBrush x:Key="TransparentBlue" Color="#00b0f0" Opacity="0.5" />
</Window.Resources>

And then only set the Backgroundof your ...Panel:

然后只设置Background你的...Panel

<StackPanel Background="{StaticResource TransparentBlue}">
    <Label Content="Hello there" FontWeight="Bold" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center"></Label>
</StackPanel>

回答by Clemens

Set the Backgroundproperty to a mutable Brush(instead of an immutable like Brushes.White). You may for example create a SolidColorBrush:

Background属性设置为可变的Brush(而不是像 一样的不可变的Brushes.White)。例如,您可以创建一个SolidColorBrush

var background = new SolidColorBrush(Colors.White);
panel.Background = background;

You may now change the Opacityproperty of that brush later on in your program.

您现在可以Opacity稍后在程序中更改该画笔的属性。

background.Opacity = 0.5;

You may also do this with any other brush like GradientBrushor ImageBrushetc.

您也可以与任何其他刷像这样做GradientBrushImageBrush

回答by LPL

Please read the Remarks of UIElement.Opacity Property

请阅读UIElement.Opacity 属性的备注

Opacity is applied from parent elements on down the element tree to child elements, but the visible effects of the nested opacity settings aren't indicated in the property value of individual child elements. For instance, if a list has a 50% (0.5) opacity and one of its list items has its own opacity set to 20% (0.2), the net visible opacity for that list item will be rendered as if it were 10% (0.1), but the property value of the list item Opacity property would still be 0.2 when queried.

不透明度从父元素沿元素树向下应用于子元素,但嵌套不透明度设置的可见效果并未在各个子元素的属性值中指示。例如,如果一个列表的不透明度为 50% (0.5),并且其中一个列表项的不透明度设置为 20% (0.2),则该列表项的净可见不透明度将呈现为 10% ( 0.1),但查询时列表项 Opacity 属性的属性值仍为 0.2。

A child control can't have more opacity then parent. I think you will have to use two separate layers, one with full opacity and one with less. Something like this

子控件的不透明度不能超过父控件。我认为你将不得不使用两个单独的图层,一个完全不透明,一个不透明。像这样的东西

<Grid>
    <StackPanel Opacity=".5" Background="whatever">
        ...
    </StackPanel>
    <StackPanel>
        <TextBlock Text="Text shown with full Opacity" />
    </StackPanel>
</Grid>

回答by Thunderburst

This worked for me in Xamarin (White transparent color) :

这在 Xamarin(白色透明色)中对我有用:

Color MyColor = Color.FromRgba(255,255,255,0.8);

Alpha seems to give better opacity than the Opacity property of controls.

Alpha 似乎比控件的 Opacity 属性提供更好的不透明度。