tags:

views:

34

answers:

1

Hey,

So I have a gauge that I've made that will be animating in either the clockwise or counter-clockwise directions. Every so often new data comes in to two dependency properties that I have set up. When the data in these properties changes, I want to do some calculations on both values to determine which direction (and by how much) the gauge's needle will rotate.

I've got the rotation code working, I've written a function (all in C#) that takes the start angle, end angle, and duration for the rotation. The rotation function works, I can put values in and watch the needle rotate.

What I can't figure out how to do is call this animation function when either of the dependency properties changes. It wouldn't be appropriate to make my rotation function static because the rotation calls may end up being instance specific.

In other words, what I'd like to achieve is PropertyChanged->calculate new positions/speed->build storyboard and run the animation.

My reasoning for having dependency properties instead of standard properties is because they are bound to outside of the control from xaml.

Thanks!

        private void AnimatePointer(double startAngle, double endAngle, TimeSpan length, string pointerName)
    {
        DoubleAnimation handRotation = new DoubleAnimation();
        handRotation.From = startAngle;
        handRotation.To = endAngle;
        handRotation.Duration = new Duration(length);
        Storyboard.SetTargetName(handRotation, pointerName);

        DependencyProperty[] propertyChain =
            new DependencyProperty[]
            {
                Rectangle.RenderTransformProperty,
                TransformGroup.ChildrenProperty,
                RotateTransform.AngleProperty
            };

        string anglePath = "(0).(1)[1].(2)";
        PropertyPath propPath = new PropertyPath(anglePath, propertyChain);
        Storyboard.SetTargetProperty(handRotation, propPath);

        Storyboard sb = new Storyboard();
        sb.Children.Add(handRotation);
        sb.Begin(this);
    }
A: 

I think you need to use PropertyChangedCallback an example

lukas
The problem with that is the PropertyChangedCallback must be a static method so I can't access my non-static stuff to animate
brandon
hmm i was thinking about wrapping this function and pass there your data to update
lukas
Maybe you have to change the design pattern?something like thishttp://www.codeguru.com/csharp/csharp/cs_misc/userinterface/print.php/c12221
lukas