Apply Opacity to LinearGradientBrush
Asked Answered
S

1

5

I have a LinearGradientBrush defined as follows. I want to use this somewhere in my xaml but I want to change the opacity in this particular case (only in this instance, not everywhere I use it). Any ideas how to accomplish this?

 <LinearGradientBrush x:Key="BlueBackgroundBrush" EndPoint="0.874,1.197" StartPoint="0.126,-0.197">
    <GradientStop Color="#1954B2" />
    <GradientStop Color="#1954B2" Offset="0.982" />
    <GradientStop Color="#FF84B2D4" Offset="0.304" />
</LinearGradientBrush>
Safier answered 27/2, 2012 at 20:2 Comment(0)
S
8

Nevermind, I figured it out. I modified from this question: Use a LinearGradientBrush in another LinearGradientBrush?

to end up with:

<GradientStopCollection  x:Key="BlueBackgroundStops">
    <GradientStop Color="#1954B2" />
    <GradientStop Color="#1954B2" Offset="0.982" />
    <GradientStop Color="#FF84B2D4" Offset="0.304" />
</GradientStopCollection>

and to use it:

 <LinearGradientBrush EndPoint="0.874,1.197" StartPoint="0.126,-0.197" 
                             GradientStops="{StaticResource BlueBackgroundStops}"
                             Opacity=".65"/>
Safier answered 27/2, 2012 at 20:32 Comment(2)
Just to clarify this, the key is to use ARGB to specify the opacity of the transparent color. Color="#FF84B2D4" in this case.Ahwaz
How do you do this in C# using the LinearGradientBrush type? For example when LinearGradientBrush is a gradient from white to red, how can I both use this gradient and the same gradient with a bit of transparency? I know the Color type has an .WithAlpha(float) method but gradient types don't have that.Anomalous

© 2022 - 2024 — McMap. All rights reserved.