none
Animate GradientStop in code behind

    Question

  • Hi. I have a simple code as follow:

    <Canvas>

    <Rectangle Width="100" Height="100">

    <Rectangle.Fill>

    <LinearGradientBrush StartPoint="0.500000,0.130412" EndPoint="0.500000,1.826112"> <LinearGradientBrush.GradientStops>

    <GradientStop x:Name="g1" Offset="0.5" Color="Red" />

    <GradientStop Color="Blue" />

    </LinearGradientBrush.GradientStops>

    </LinearGradientBrush>

    </Rectangle.Fill>

    </Rectangle>

    </Canvas>

    if I want to animate the gradientstop named "g1" in xaml, I can simply assign like

    <ColorAnimation Storyboard.TargetName="g1" ....

    Due to requirement, I need to assign the animation in code behind. I'm able to retrieve the element of that first gradientstop but I can't retrieve its name in order to be assigned to storyboard object as follow:

    Storyboard.SetTargetProperty(colorAnimation, ...)

    Anyone can guide me how to assign the animation, say, color animation to that first gradientstop element in code behind?

    thank you in advance.

    waifoong

    Friday, March 17, 2006 9:52 AM

Answers

  • This should work:

    PropertyPath myPropertyPath = new PropertyPath("(0)", new DependencyProperty[] { GradientStop.ColorProperty });
    Storyboard.SetTargetProperty(colorAnimation, myPropertyPath);
    Storyboard.SetTargetName(colorAnimation, "g1");

    Thanks,
    Ed

    Saturday, March 18, 2006 2:48 AM
    Moderator

All replies

  • This should work:

    PropertyPath myPropertyPath = new PropertyPath("(0)", new DependencyProperty[] { GradientStop.ColorProperty });
    Storyboard.SetTargetProperty(colorAnimation, myPropertyPath);
    Storyboard.SetTargetName(colorAnimation, "g1");

    Thanks,
    Ed

    Saturday, March 18, 2006 2:48 AM
    Moderator
  • Thanks Ed, the code works fine.

    In addition to your code, what is the first parameter "(0)" means? when to pass what value? I can't understand that parameter. for some other sample code, it passes, say, "0.1" and some others are different.

    Thanks again.

    waifoong

    Monday, March 20, 2006 5:05 AM