WPF: Change color in a style's gradient using c#

M

moondaddy

I have a custom control where users can set the backcolor of one of it's UI
elements via a DP I created. One of the colors they can pick is a brush
called CustomGradient where I would set the DP to this brush. Then from a
color picker I would let them pick a solid color brush and set 2 of the
gradient stops to this new value.



So for example, here's the style:



<LinearGradientBrush x:Key="CustomGradientColor" EndPoint="0.5,1"
StartPoint="0.5,0">

<GradientStop Color="#FFFFFFFF" Offset="0"/>

<GradientStop Color="#FFFFFFFF" Offset="1"/>

<GradientStop Color="#FF000000" Offset="0.755"/>

<GradientStop Color="#FF000000" Offset="0.550"/>

</LinearGradientBrush>



And lets say they pick a new color to replace "#FF000000":



Brush newBrush = Brushes.Red;



Using c#, how can I change 'this.BackColor' to CustomGradientColor and swap
out "#FF000000" with newBrush in CustomGradientColor?



Thanks.
 
M

moondaddy

Thanks. Yes I meant Background color and not backcolor. I'll reply to your
other post.

Peter Duniho said:
[...]
Using c#, how can I change 'this.BackColor' to CustomGradientColor and
swap
out "#FF000000" with newBrush in CustomGradientColor?

Can you be more specific about what class's background color you're trying
to change? I admit, I'm still pretty new to WPF, but my understanding is
that you use the Control.Background property change the background. I
couldn't find a WPF class that had a BackColor property.

If you were in fact trying to change the Background property of a Control
instance, then I would say you'd simply want to create a new
LinearGradientBrush instance based on the input you want, and assign that
instance to the Background property of your Control. If you want the new
brush to have the same values as the old, but with some values replaced,
then you'd just copy the values you want to preserve and provide new
values for the ones you want to replace.

For example, get the GradientStops collection from the original brush,
modify the GradientStop members of that collection that you want to
modify, and then use the modified collection along with the original start
and end points from your original brush, passing all three things to the
appropriate LinearGradientBrush constructor.

I suppose it's possible you could just modify the individual GradientStop
instances in the original brush's GradientStops collection. Like I said,
I'm new to WPF. It depends on whether WPF is using the collection as a
way of passing data, or if you get the actual collection being used by the
brush when you retrieve it from the GradientStops property. In my
experience, it's more common for an API like this to do the former, but it
could in fact be doing the latter for all I know.

Pete
 
M

moondaddy

This looks good and will give it a try. If I'm able to get the value of the
gradientstops, then I could get the values of 1 and 4, and replace 2 and 3,
and with those, create a new LinearGradientBrush. either way may be fine.
Thanks for all the ideas.


[...]
I suppose it's possible you could just modify the individual GradientStop
instances in the original brush's GradientStops collection.

So I had a moment and decided to play with this a little. As near as I
can tell, the brush instance is completely mutable. So, if you've already
got a LinearGradientBrush assigned, you can just access individual
GradientStop instances from the GradientStops collection and modify them,
and the modification will be reflected immediately.

Even better, you don't even need to provide a new GradientStop. You can
just change the Color property of the GradientStop and it will work fine.

So, let's assume you've got an existing LinearGradientBrush, and you want
to change its color to match an existing SolidBrush (why you're not just
dealing directly with colors, I'm not sure...but that's what you said you
want to do, so okay... :) ). Then the code might look something like
this:

LinearGradientBrush brushTarget = /* initialized from the appropriate
control, user element, etc. */;
SolidColorBrush brushSource = /* initialized as desired...you said
from a color picker, for example */;

// Let's assume you want to change the color of the
// second pair of stops in the example you posted...

brushTarget.GradientStops[2].Color = brushSource.Color;
brushTarget.GradientStops[3].Color = brushSource.Color;

After that code, the two stops that were originally initialized to
#FF000000 will now have the new color assigned from the solid brush.

Hope that helps.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top