L
L
llexus2014-11-11 12:45:34
WPF
llexus, 2014-11-11 12:45:34

LongListSelector (or ListBox) scrolling animation?

Hello. The task set to itself the following:
Is LongListSelector. As soon as the data is loaded into it, you need to scroll from the code to a specific element.
I used ScrollTo. The function works, but it seemed to me that this was not enough. I decided to google how to animate this thing beautifully. There is some deprecate function AnimateTo. I tried it - the effect is the same as ScrollTo, no animations.
Further googling gave me the idea to add my own DependencyProperty to the vertical scroll offset:

public partial class LongListSelector : Control
    {
        public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);

                    if (childOfChild != null)
                        return childOfChild;
                }
            }

            return null;
        }

        public static readonly DependencyProperty VerticalOffsetProperty = DependencyProperty.Register("VerticalOffset",
           typeof(double), typeof(LongListSelector), new PropertyMetadata(VerticalOffsetPropertyChanged));
        public double VerticalOffset
        {
            get { return (double)this.GetValue(VerticalOffsetProperty); }
            set { this.SetValue(VerticalOffsetProperty, value); }
        }
        private static void VerticalOffsetPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            LongListSelector cThis = sender as LongListSelector;
            ScrollViewer LScrollViewer = FindVisualChild<ScrollViewer>(cThis);
            LScrollViewer.ScrollToVerticalOffset((double)args.NewValue);
        }
        public static readonly DependencyProperty ContentAreaProperty = DependencyProperty.Register("ContentArea", typeof(object), typeof(LongListSelector), null);
        public object ContentArea
        {
            get { return (object)GetValue(ContentAreaProperty); }
            set { SetValue(ContentAreaProperty, value); }
        }
...

Made. I'm trying to scroll somewhere...
DoubleAnimation verticalAnimation = new DoubleAnimation();
            verticalAnimation.From = 0;
            verticalAnimation.To = 25;
            verticalAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(5000));
            CubicEase ea = new CubicEase();
            ea.EasingMode = EasingMode.EaseInOut;
            verticalAnimation.EasingFunction = ea;
            var sb = new Storyboard();
            sb.Children.Add(verticalAnimation);
            Storyboard.SetTarget(verticalAnimation, buddies);
            Storyboard.SetTargetProperty(verticalAnimation, new PropertyPath(LongListSelector.VerticalOffsetProperty));
            sb.Begin();

Everything is cool, smoothly scrolling. And now my own question:
How to correctly define verticalAnimation.To ? For example, for a huge list, the scrolling interval (ScrollViewer.ScrollableHeight) turned out to be from 0 to 119. Why 119? How is it measured? In fact, the entire list occupies twenty screens, that is, many thousands of pixels. And how do I calculate this value if, for example, I want to scroll to N items in the list?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question