UWP trying to animate scrollviewer with attached dependency propert

后端 未结 1 455
忘掉有多难
忘掉有多难 2021-01-07 12:13

Am trying to animate horizontal offset of scrollviewer in UWP.but the attached properties are not being identified by animation target.



        
1条回答
  •  一整个雨季
    2021-01-07 12:44

    You cannot animate a custom attached property because of an existing limitation of the Windows Runtime XAML implementation. Please see Animating XAML attached properties section in MSDN document.

    For your case, you could make a custom usercontrol and define a dependency property for this usercontrol. Then, you could animate this dependency property.

    In this property's PropertyChangedCallback handler method, you could change ScrollViewer's Horizontalofset.

    Please refer to my following code sample for details:

    
    
    
        
            
        
    
    

    public sealed partial class MyUserControl1 : UserControl
    {
        public MyUserControl1()
        {
            this.InitializeComponent();
        }
    
        public double Horizontalofset
        {
            get { return (double)GetValue(HorizontalofsetProperty); }
            set { SetValue(HorizontalofsetProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Horizontalofset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HorizontalofsetProperty =
            DependencyProperty.Register("Horizontalofset", typeof(double), typeof(MyUserControl1), new PropertyMetadata(0,PropertyChangedCallback));
    
        public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var distance = (d as MyUserControl1).scrolviewer.ScrollableWidth;
            if (distance > (double)e.NewValue)
            {
                var ret = (d as MyUserControl1).scrolviewer.ChangeView((double)e.NewValue, (d as MyUserControl1).scrolviewer.VerticalOffset, (d as MyUserControl1).scrolviewer.ZoomFactor);
                Debug.WriteLine(ret);
            }
    
        }
    }
    
    
            
                
                    
                
            
            
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        animation.Begin();
    
    }
    

    Please note that you would need to enable EnableDependentAnimation, if not, your animation will not work.

    0 讨论(0)
提交回复
热议问题