如何使用故事板为用户控件依赖项属性设置动画?

时间:2013-09-01 07:57:19

标签: windows-8 windows-runtime dependency-properties

我正在尝试基于故事板滚动我的用户控件(包含列表视图)。但首先要做的是---我需要能够使用故事板为我的自定义属性设置动画。

我可以动画我的自定义属性ScrollPos,但我的set()函数根本没有触发。

如何检测到它已更改,以便我可以执行实际滚动?

我的用户控件:

    public sealed partial class MyCellListView : UserControl
    {
        public MyCellListView()
        {
            this.InitializeComponent();
            this.DataContext = this; 
        }

        public const string ScrollPosPropertyTag = "ScrollPos";
        public static readonly DependencyProperty ScrollPosProperty =
           DependencyProperty.Register(
               ScrollPosPropertyTag,
               typeof(double),
               typeof(MyCellListView),
               new PropertyMetadata((double)0));
        public double ScrollPos
        {
          get
          {
            return (double)GetValue(ScrollPosProperty);
          }
          set
          {
            // If I set a breakpoint here, it only gets hit on creation to set
            // the initial value of ScrollPos --- not during the storyboard.
            SetValue(ScrollPosProperty, value);
          }
        }
    }

它所在的页面:

    public MyPage // contains MyCellListView
    {
        private void startScrolling()
        {
          // Calculate target offset
          double targetOffset = m_teacher.getNumUniqueCues();

          // Create animation and storyboard
          DoubleAnimation animation = new DoubleAnimation();

          ExponentialEase ee = new ExponentialEase();
          ee.EasingMode = EasingMode.EaseIn;
          ee.Exponent = 3;
          animation.EasingFunction = ee;

          animation.Duration = new Duration(new TimeSpan(0, 0, 30));
          animation.From = 0;
          animation.To = targetOffset;

          Storyboard.SetTarget(animation, m_listView);
          Storyboard.SetTargetProperty(animation,
            // Works fine here for "(MyCellListView.Opacity)");
            "(MyCellListView.ScrollPos)");
          Storyboard storyboard = new Storyboard();
          storyboard.Children.Add(animation);

          // Need this or the dependent property won't get animated
          animation.EnableDependentAnimation = true;

          storyboard.Begin();
        }
    }

1 个答案:

答案 0 :(得分:0)

答案很简单。详见文档:http://msdn.microsoft.com/en-us/library/windows/apps/hh920267.aspx

    public const string ScrollPosPropertyTag = "ScrollPos";
    public static readonly DependencyProperty ScrollPosProperty =
       DependencyProperty.Register(
           ScrollPosPropertyTag,
           typeof(double),
           typeof(MyCellListView),
           new PropertyMetadata((double)0, new PropertyChangedCallback(OnScrollPosChanged)));
    public double ScrollPos
    {
      get
      {
        return (double)GetValue(ScrollPosProperty);
      }
      set
      {
        SetValue(ScrollPosProperty, value);
      }
    }

    private static void OnScrollPosChanged(DependencyObject dob,
      DependencyPropertyChangedEventArgs args)
    {
      if (dob != null)
      {
        MyCellListView listView = dob as MyCellListView;
        listView.DoStuffHere();
      }
    }