views:

47

answers:

1

Before I start, I have this code inside of a Custom Usercontrol:

private DependencyProperty _rotation = DependencyProperty.Register("Rotation", typeof(double), typeof(MyControl),
                                       new PropertyMetadata(new PropertyChangedCallback(RotationPropertyChanged)));
    public double Rotation
    {
        get { return (double)GetValue(_rotation); }
        set { SetValue(_rotation, value); }
    }
    public static void RotationPropertyChanged(DependencyObject obj, System.Windows.DependencyPropertyChangedEventArgs e)
    {
        //How can I start Animation, as I'm in a Static method?
    }

The Properties are getting set correctly, and my RotationPropertyChanged Function is being called correctly as well. As you can see, my comment inside that method is my question. Since this handler NEEDS to be static (VS Told me so), How do I access Non-Static things, such as a storyboard so I can start animation?

To elaborate on the databinding:

My Viewmodel is updating a property(located in that same viewmodel) which is databound to this dependency property via Xaml. I wish I didn't have to use this callback, but the property wont be changed without it.

Thanks

+3  A: 

You can just cast the DependencyObject passed into the static event handler to your control type and then call an instance method on it. I think this is a pretty common pattern with dependency properties in Silverlight/WPF:

private DependencyProperty _rotation = DependencyProperty.Register(
    "Rotation",
    typeof(double), 
    typeof(MyControl),
    new PropertyMetadata(new PropertyChangedCallback(RotationPropertyChanged)));

public double Rotation
{
    get { return (double)GetValue(_rotation); }
    set { SetValue(_rotation, value); }
}

public static void RotationPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    ((MyControl)obj).RotationPropertyChanged(e);
}

private void RotationPropertyChanged(DependencyPropertyChangedEventArgs e)
{
    // Start your animation, this is now an instance method
}
Dan Auclair
Perfect!!Thanks!
Peanut