By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,421 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,421 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
--
mo*******@newsgroup.nospam
Jul 3 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Thu, 03 Jul 2008 16:03:06 -0700, moondaddy <mo*******@newsgroup.nospam>
wrote:
[...]
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
Jul 3 '08 #2

P: n/a
On Thu, 03 Jul 2008 16:33:59 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
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
Jul 4 '08 #3

P: n/a
Thanks. Yes I meant Background color and not backcolor. I'll reply to your
other post.

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 03 Jul 2008 16:03:06 -0700, moondaddy <mo*******@newsgroup.nospam>
wrote:
>[...]
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

Jul 4 '08 #4

P: n/a
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.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 03 Jul 2008 16:33:59 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
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
Jul 4 '08 #5

P: n/a
On Thu, 03 Jul 2008 17:48:00 -0700, moondaddy <mo*******@newsgroup.nospam>
wrote:
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.
Glad (and surprised :) ) I could help.

I should clarify: while I said before that you might have to create a new
brush, it turns out that you can just modify the GradientStops collection
in the existing brush. If there's not already a LinearGradientBrush
assigned to the Background property, then yes...you'll have to create a
new one. But otherwise, you can just modify the brush that's already
there.

Pete
Jul 4 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.