7

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.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

3 Answers3

7

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" />
Nethemas
  • 136
  • 1
  • 9
3

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>
Lorentz Vedeler
  • 5,101
  • 2
  • 29
  • 40
0

Well, I think I found it! Thanks to other work I've done, I came up with a DynamicResourceBinding concept (StaticResourceBinding too!) which you can use a converter to transform the brush in any way you want.

Here's a link to that page here on StackOverflow where I do this for both Dynamic and Static resources...

Post 33816511: How to create a DynamicResourceBinding

Community
  • 1
  • 1
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286