472,958 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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

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
5 7061
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
I am trying to get a gradient filter on a table item. I am using... <td style="width: 309px; filter: DXImageTransform.Microsoft.Gradient(EndColorStr=#c0cfe2, StartColorStr=#ffffff,...
11
by: Maximus | last post by:
Hi all, Has anyone been able to reterive the gradient selection colors used in outlook 2003? Basically, I have a grid showing a list of records and my client wants the selected row to have the...
6
by: moondaddy | last post by:
I'm writing a windows app in WPF and want to change the shape of a thumb to an ellipse. Is this possible? also, the edges of the thumb are beveled. Is it possible to change this to a flat look...
11
by: Don | last post by:
I have a WPF application in VB in VSTS 2008 RTM. I am trying to "blink" (momentarily clear) a field of data if the data is reloaded from the database to give the user some visual indication of the...
8
by: moondaddy | last post by:
I'm posting code for a user control ( FunctionConnectorSelector) below which has 3 content controls in it. each content control uses a style from a resource dictionary merged into the app.xaml...
3
by: Just_a_fan | last post by:
I have been through two books where they talk about the (almost) parallel control set for WPF and VB. They go through the small difference and talk about everything going on a form but what they...
7
by: Linda Liu[MSFT] | last post by:
Hi George, I have downloaded your sample solution and built it on my machine. I got a compilation error indicating that the type of "CustomResources" doesn't exist in the following xaml code: ...
23
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I...
0
by: furqanms | last post by:
Hello, I am new to WPF ,I am developing touch screen system using WPF. I am facing problem in Binding relative reference. Here is my code : <UserControl x:Class="uctlBrowser" ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.