Esempio n. 1
0
        public static void ScrollItem(ListViewBase control, int indexDelta)
        {
            if (control == null || control.Items == null)
            {
                return;
            }

            var scrollViewer = VisualTreeUtilities.GetVisualChild <ScrollViewer>(control);

            var p          = new Point(Window.Current.Bounds.Width / 2, 10);
            var transform  = control.TransformToVisual(Window.Current.Content);
            var checkPoint = transform.TransformPoint(p);

            var q = from lvi in VisualTreeHelper.FindElementsInHostCoordinates(checkPoint, scrollViewer).OfType <ListViewItem>()
                    where lvi.Content != null
                    select lvi.Content;

            var item = q.FirstOrDefault();

            if (item == null)
            {
                return;
            }

            var index         = control.Items.IndexOf(item);
            var nextItemIndex = index + indexDelta;

            if (index != -1 && nextItemIndex >= 0 && nextItemIndex < control.Items.Count)
            {
                var nextItem = control.Items[nextItemIndex];
                control.ScrollIntoView(nextItem, ScrollIntoViewAlignment.Leading);
            }
        }
Esempio n. 2
0
        void _element_LayoutUpdated(object sender, object e)
        {
            if (!_loaded)
            {
                return;
            }

            if (_scrollViewer == null)
            {
                _scrollViewer = VisualTreeUtilities.GetVisualChild <ScrollViewer>(_element);
                if (_scrollViewer == null)
                {
                    return;
                }
            }

            var canSetHorizontalOffset = _scrollViewer.ScrollableWidth > 0D &&
                                         _scrollableWidth > 0D &&
                                         Math.Abs(1D - (_scrollViewer.ScrollableWidth / _scrollableWidth)) < Tolerance;

            if (canSetHorizontalOffset && !_horizontalSet)
            {
                _scrollViewer.ChangeView(_horizontalOffset, null, null, true);
                _horizontalSet = true;
            }

            var canSetVerticalOffset = _scrollViewer.ScrollableHeight > 0D &&
                                       _scrollableHeight > 0D &&
                                       Math.Abs(1D - (_scrollViewer.ScrollableHeight / _scrollableHeight)) < Tolerance;

            if (canSetVerticalOffset && !_verticalSet)
            {
                _scrollViewer.ChangeView(null, _verticalOffset, null, true);
                _verticalSet = true;
            }

            if (_verticalSet && _horizontalSet)
            {
                _element.LayoutUpdated -= _element_LayoutUpdated;
            }
        }
Esempio n. 3
0
        public void Save([NotNull] Dictionary <string, object> pageState, string keyPrefix)
        {
            if (pageState == null)
            {
                throw new ArgumentNullException("pageState");
            }

            if (_scrollViewer == null)
            {
                _scrollViewer = VisualTreeUtilities.GetVisualChild <ScrollViewer>(_element);
                if (_scrollViewer == null)
                {
                    return;
                }
            }

            pageState[keyPrefix + "ScrollableWidth"]  = _scrollViewer.ScrollableWidth;
            pageState[keyPrefix + "ScrollableHeight"] = _scrollViewer.ScrollableHeight;
            pageState[keyPrefix + "HorizontalOffset"] = _scrollViewer.HorizontalOffset;
            pageState[keyPrefix + "VerticalOffset"]   = _scrollViewer.VerticalOffset;
        }