2

My app has a text with Gradient on a canvas and I move it around&change the Opacity

textblock.Foreground = new SolidColorBrush(
    Color.FromArgb((Byte)opacitiycounter, 255, 255, 0));

That works great- but for SolidColorBrush. The challange I got myself into was to change the opacity of a given Gradient:

public Points(TextBlock t, GradientBrush color) {
    color.Opacity = opacitiycounter;
    t.Foreground = color;
}

The opacity function doesn't seem to work and I can't find another way that I can control the given Gradient Opacity, do I need to create entire new Gradient each time with the old values and change the alpha at each point? if thats the case how do I even copy a gradient?

edit: I'm trying to edit existing gradient dynamically in WPF.

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
SpoocyCrep
  • 604
  • 7
  • 23
  • Possible duplicate of [Linear Gradient Brush Fade WPF](https://stackoverflow.com/questions/3294250/linear-gradient-brush-fade-wpf) – Freggar Jun 12 '18 at 08:27
  • @Freggar that solves one of the problems of changing the gradient alpha dynamically but I still don't know how to use the given gradient and change the alpha for it\duplicate the colors of the given gradient – SpoocyCrep Jun 12 '18 at 08:36

1 Answers1

3

From MSDN:

The value of the Opacity property is expressed as a value between 0.0 and 1.0.

So this should work:

color.Opacity = (double)opacitycounter / 255;
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • That works! I feel stupid for that sorry. However, I encounter another problem- now all the texts with the gradient change opacity the same (as they point to the same gradient), is there a way to duplicate\copy gradient to a new gradient? EDIT: found out there is a clone function nevermind thanks! – SpoocyCrep Jun 12 '18 at 09:41