views:

36

answers:

3

How can I set the opacity of the background for a groupbox etc.

The code beneath doesn't compile:

<Style TargetType="GroupBox">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Opacity="0.5">White</SolidColorBrush>
        </Setter.Value>
    </Setter>
</Style>
+2  A: 

Your code isn't compiling not because of the opacity, but because of the value "White". You have to apply this to the brush Color.

You can use:

<SolidColorBrush Opacity="0.5" Color="White" />

or

<SolidColorBrush Opacity="0.5">
    <SolidColorBrush.Color>White</SolidColorBrush.Color>
</SolidColorBrush>
Jay
Oh yeah, I see... Was exactly what I look for. Have read your conversation beneath, and IMO the solution above (first one-liner) is the most clear for me, but thanks to both of you!
kahr
A: 

Opacity is a property of Groupbox itself, not of its background.

Try

<Style TargetType="GroupBox">
    <Setter Property="Background" Value="White"/>
    <Setter Property="Opacity" Value="0.5"/>
</Style>

Or you can style the GroupBox at the place where you use it, as in @Jay's answer.

If you'd really like to change only the background opacity, use the following:

<Style TargetType="GroupBox">
    <Setter Property="Background" Value="#80ffffff"/>
</Style>
Vlad
Controls have opacity, true, but so do brushes. Big difference between making the entire control semitransparent vs. the background, for which the OP is asking.
Jay
If we want transparency on the background, we need to use numeric value for the colour instead of just `White`.
Vlad
No you don't. `<SolidColorBrush Opacity="0.5" Color="White" />` is just fine.
Jay
@Jay: Yes, but this way you need an extra `SolidColorBrush`, which is not needed in my proposal. Now, it's more matter of personal taste. I would define a color with a name and put it as a `StaticResource`.
Vlad
There is no extra brush; the background property is *always* a brush. You can make the `SolidColorBrush` a resource as easily as a color.
Jay
I go with Jay's answer. As per the requirement, he only want to reduce the opacity of the background color not the control's opacity. If, we set the Opacity property of the groupbox, it not only updates the color, but everything.
Avatar
@Jay: of course, but I am saving typing in extra (and IMHO unnecessary) code. Every extra line of code makes maintenance more complicated, and is a possible source of mistakes.
Vlad
@Jay: having a `Brush` as a globally accessible resource will crash if your application uses more than one UI thread.
Vlad
@Avatar: You are right, but see the last part of my answer.
Vlad
I hear you, Vlad, but `<SolidColorBrush Opacity="0.5" Color="White" />` is one line and is very clear to understand (moreso, I'd argue than `#80ffffff`). What happens when you try to change or animate the background opacity? The values are always half what you expect and you waste 15 minutes figuring out that it is because the opacity is being multiplied against the brush color opacity. Also, a `Brush` (except `VisualBrush`) is a `Freezable` -- as a read-only resource it is perfectly thread safe.
Jay
As I said above, I also best like Jays version, but thanks anyway! :o)
kahr
@Jay: Your code requires extra `<Setter.Value>...</Setter.Value>`. Which is not bad itself, just longer. Of course, if you need to work with the background opacity, you need a more elaborate code, no doubt about that. [`Freezable` is thread safe if you don't forget to freeze it manually -- from experience of my colleagues with .NET 3.5.]
Vlad
A: 

you can set the opacity to whatever you want directly on the color. the first two hex numbers control the "alpha" of the brush. 7F is 50%

<SolidColorBrush  x:Key="MyBrush" Color="#7FFFFFFF"/>

so, your style would look like this:

<Style TargetType="GroupBox">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="#7FFFFFFF"/>
        </Setter.Value>
    </Setter>
</Style>
Muad'Dib
Thanks also for this answer... :o)
kahr