brandon
brandon

Reputation: 374

Trouble with static vs non-static stuff in dependency properties... should I even be using DPs?

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);
    }

Upvotes: 3

Views: 2046

Answers (2)

brandon
brandon

Reputation: 374

I was being silly and overlooked the fact that the first parameter to all of the static callback methods is the instance that the method is being called on. It simply needs to be casted to whatever your type is then the properties can be accessed through that.

Upvotes: 5

Lukasz Madon
Lukasz Madon

Reputation: 15004

I think you need to use PropertyChangedCallback an example

Upvotes: 1

Related Questions