Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you apply an opacity to a system-defined brush?

I know the WPF brush class has an Opacity property. We have a need to use a system-defined brush but with half the opacity. We'd like to do something like this (this is obviously fake code)...

<Border Background="{DynamicResource {x:Static SystemColors.HighlightBrushKey}, Opacity=0.5}" />

We of course can't change the opacity on a system-defined brush directly because that would mess up everywhere it's used.

What we'd like to do is to somehow define a converter which we take one brush and returns a copy of it with the opacity changed, but since this isn't a binding, we don't know where/how to apply a converter. Plus, if it's a dynamic resource, we need to make sure it tracks changes.

We also can't simply set the opacity on the Border directly either as then all of its children also have the same reduced opacity.

Our current work-around is instead of putting the content directly in the border, we put the border and its contents as siblings in a grid, then we do set the opacity on the border. Since the content is now on top of, instead of inside the border, it isn't affected by the opacity. It just means we've added extra stuff into the visual tree which is annoying, but it does work. It would be much better if we could simply adjust the opacity of a (copy of a) system brush right in the XAML.

like image 590
Mark A. Donohoe Avatar asked May 23 '15 09:05

Mark A. Donohoe


People also ask

How do you make a brush opaque in Photoshop?

Select the part of the image you want to make into a scatter brush, copy and paste it to a New Layer and turn it to black and white with good contrast. Remember that black will produce 100% of the chosen opacity and white will be completely transparent. Gray will be semi transparent.

Why is my brush not full opacity?

The brush opacity is set to 100% by default. To change brush opacity, click the opacity drop-down menu. Then, drag the slider to the left to decrease the opacity or to the right to increase it.

Why does my brush have opacity in Photoshop?

With the Brush Tool selected, the Opacity and Flow options appear in the Options Bar. Opacity controls the translucency of the brush color as we paint. When the Opacity value is set to 100% (the default value), the brush color is opaque, completely blocking anything below the area we're painting over from view.

What is opacity mask in WPF?

Opacity masks enable you to make portions of an element or visual either transparent or partially transparent. To create an opacity mask, you apply a Brush to the OpacityMask property of an element or Visual.


2 Answers

A bit late, but for the sake of others...

You can create derivative solid color brushes with new opacities. To do this you simply borrow the color from the original brush used as the binding source, then set a new opacity.

<SolidColorBrush Color="{Binding Color, Source={StaticResource blue-light}}" Opacity="0.5" />
like image 83
Nethemas Avatar answered Oct 05 '22 19:10

Nethemas


Maybe you could try creating a new brush based on the system color in stead of using the system brush directly, like this:

<Border>
    <Border.Background>
        <SolidColorBrush 
            Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"
            Opacity="0.5" />
    </Border.Background>
</Border>
like image 39
Lorentz Vedeler Avatar answered Oct 05 '22 21:10

Lorentz Vedeler