Exemplo n.º 1
0
        public void Attach(DependencyObject associatedObject)
        {
            this.AssociatedObject = associatedObject;
            ListViewBase dataView = this.AssociatedObject as ListViewBase;

            if (dataView != null)
            {
                ScrollViewer scrollViewer = dataView.GetDescendantsOfType <ScrollViewer>().FirstOrDefault();
                if (scrollViewer != null)
                {
                    this.AttachScrollBar(scrollViewer);
                }
                else
                {
                    RoutedEventHandler loadedHandler = null;
                    loadedHandler = (sender, e) =>
                    {
                        scrollViewer = dataView.GetDescendantsOfType <ScrollViewer>().FirstOrDefault();
                        this.AttachScrollBar(scrollViewer);
                        dataView.Loaded -= loadedHandler;
                    };
                    dataView.Loaded += loadedHandler;
                }
            }
        }
Exemplo n.º 2
0
        public static void ScrollIntoViewSmoothly(this ListViewBase listViewBase, object item, ScrollIntoViewAlignment alignment)
        {
            if (listViewBase == null)
            {
                throw new ArgumentNullException(nameof(listViewBase));
            }

            ScrollViewer scrollViewer = listViewBase.GetDescendantsOfType <ScrollViewer>().First();

            double originHorizontalOffset = scrollViewer.HorizontalOffset;
            double originVerticalOffset   = scrollViewer.VerticalOffset;

            EventHandler <object> layoutUpdatedHandler = null;

            layoutUpdatedHandler = delegate
            {
                listViewBase.LayoutUpdated -= layoutUpdatedHandler;

                double targetHorizontalOffset = scrollViewer.HorizontalOffset;
                double targetVerticalOffset   = scrollViewer.VerticalOffset;

                EventHandler <ScrollViewerViewChangedEventArgs> scrollHandler = null;
                scrollHandler = (sender, e) =>
                {
                    scrollViewer.ViewChanged -= scrollHandler;

                    scrollViewer.ChangeView(targetHorizontalOffset, targetVerticalOffset, null);
                };
                scrollViewer.ViewChanged += scrollHandler;;

                scrollViewer.ChangeView(originHorizontalOffset, originVerticalOffset, null, true);
            };
            listViewBase.LayoutUpdated += layoutUpdatedHandler;

            listViewBase.ScrollIntoView(item, alignment);
        }