Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set stackpanel opacity without affecting on child control

Tags:

wpf

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)

Please give me proper solution for this.

Thank you.

like image 581
jadav suresh Avatar asked Aug 01 '13 11:08

jadav suresh


People also ask

How do you make a button transparent in WPF?

You can set a control's background transparent by setting the Background property to null, using the following code: <Button Name="Button1" Height="30" Width="300" BorderBrush="Yellow" Background="{x:Null}" />

What is Stack Panel?

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.

What is Stack Panel in c#?

A StackPanel places child elements in a vertical or horizontal stack. It is one of the popular panels because of its simplicity. By default, a StackPanel's child element grows from the top of the panel to the bottom, in other words in vertical orientation.

What does StackPanel do in WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.


2 Answers

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

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

And then only set the Background of your ...Panel:

<StackPanel Background="{StaticResource TransparentBlue}">
    <Label Content="Hello there" FontWeight="Bold" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center"></Label>
</StackPanel>
like image 75
Sonhja Avatar answered Oct 08 '22 20:10

Sonhja


Please read the Remarks of UIElement.Opacity Property

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.

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>
like image 38
LPL Avatar answered Oct 08 '22 21:10

LPL