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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moondaddy

    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:



    <LinearGradient Brush x:Key="CustomGr adientColor" EndPoint="0.5,1 "
    StartPoint="0.5 ,0">

    <GradientStop Color="#FFFFFFF F" Offset="0"/>

    <GradientStop Color="#FFFFFFF F" Offset="1"/>

    <GradientStop Color="#FF00000 0" Offset="0.755"/>

    <GradientStop Color="#FF00000 0" Offset="0.550"/>

    </LinearGradientB rush>



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



    Brush newBrush = Brushes.Red;



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



    Thanks.


    --
    moondaddy@newsg roup.nospam


  • Peter Duniho

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

    On Thu, 03 Jul 2008 16:03:06 -0700, moondaddy <moondaddy@news group.nospam>
    wrote:
    [...]
    Using c#, how can I change 'this.BackColor ' to CustomGradientC olor and
    swap
    out "#FF000000" with newBrush in CustomGradientC olor?
    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.Backgro und 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
    LinearGradientB rush 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 LinearGradientB rush 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

    Comment

    • Peter Duniho

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

      On Thu, 03 Jul 2008 16:33:59 -0700, Peter Duniho
      <NpOeStPeAdM@nn owslpianmk.comw rote:
      [...]
      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 LinearGradientB rush 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 LinearGradientB rush, 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:

      LinearGradientB rush 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.Gra dientStops[2].Color = brushSource.Col or;
      brushTarget.Gra dientStops[3].Color = brushSource.Col or;

      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

      Comment

      • moondaddy

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

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

        "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
        news:op.udqo2xn d8jd0ej@petes-computer.local. ..
        On Thu, 03 Jul 2008 16:03:06 -0700, moondaddy <moondaddy@news group.nospam>
        wrote:
        >
        >[...]
        >Using c#, how can I change 'this.BackColor ' to CustomGradientC olor and
        >swap
        >out "#FF000000" with newBrush in CustomGradientC olor?
        >
        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.Backgro und 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
        LinearGradientB rush 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 LinearGradientB rush 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

        Comment

        • moondaddy

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

          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 LinearGradientB rush. either way may be fine.
          Thanks for all the ideas.


          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.udqqh0d u8jd0ej@petes-computer.local. ..
          On Thu, 03 Jul 2008 16:33:59 -0700, Peter Duniho
          <NpOeStPeAdM@nn owslpianmk.comw rote:
          [...]
          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 LinearGradientB rush 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 LinearGradientB rush, 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:

          LinearGradientB rush 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.Gra dientStops[2].Color = brushSource.Col or;
          brushTarget.Gra dientStops[3].Color = brushSource.Col or;

          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


          Comment

          • Peter Duniho

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

            On Thu, 03 Jul 2008 17:48:00 -0700, moondaddy <moondaddy@news group.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 LinearGradientB rush. 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 LinearGradientB rush
            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

            Comment

            Working...