在静态方法中使用依赖项属性回调和动画进行数据绑定

Pea*_*nut 2 data-binding silverlight windows-phone-7

在开始之前,我在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?
    }
Run Code Online (Sandbox Code Playgroud)

属性设置正确,我的RotationPropertyChanged函数也被正确调用.如您所见,我在该方法中的评论是我的问题.由于这种处理器需要是静态的(VS这么告诉我的),我如何访问非静态的东西,比如故事板,所以我可以开始动画?

详细说明数据绑定:

我的视图模型正在更新一个属性(位于同一视图模型),这是数据绑定通过XAML中这种依赖性属性.我希望我不必使用这个回调,但没有它就不会改变属性.

谢谢

Dan*_*air 8

您可以将DependencyObject传递给静态事件处理程序转换为您的控件类型,然后在其上调用实例方法.我认为这是一个非常常见的模式,在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
}
Run Code Online (Sandbox Code Playgroud)