BaconographyWP8.Common.ReorderListBox.GetViewIndexRange C# (CSharp) Method

GetViewIndexRange() public method

Gets the indices of the first and last items in the view based on the current scroll position.
public GetViewIndexRange ( bool includePartial, int &firstIndex, int &lastIndex ) : void
includePartial bool True to include items that are partially obscured at the top and bottom, /// false to include only items that are completely in view.
firstIndex int Returns the index of the first item in view (or -1 if there are no items).
lastIndex int Returns the index of the last item in view (or -1 if there are no items).
return void
        public void GetViewIndexRange(bool includePartial, out int firstIndex, out int lastIndex)
        {
            if (this.Items.Count > 0)
            {
                firstIndex = 0;
                lastIndex = this.Items.Count - 1;

                if (this.scrollViewer != null && this.Items.Count > 1)
                {
                    Thickness scrollViewerPadding = new Thickness(
                        this.scrollViewer.BorderThickness.Left + this.scrollViewer.Padding.Left,
                        this.scrollViewer.BorderThickness.Top + this.scrollViewer.Padding.Top,
                        this.scrollViewer.BorderThickness.Right + this.scrollViewer.Padding.Right,
                        this.scrollViewer.BorderThickness.Bottom + this.scrollViewer.Padding.Bottom);

                    GeneralTransform scrollViewerTransform = this.scrollViewer.TransformToVisual(
                        Application.Current.RootVisual);
                    Rect scrollViewerRect = scrollViewerTransform.TransformBounds(
                        new Rect(new Point(0, 0), this.scrollViewer.RenderSize));

                    Point topPoint = ReorderListBox.GetHostCoordinates(new Point(
                        scrollViewerRect.Left + scrollViewerPadding.Left,
                        scrollViewerRect.Top + scrollViewerPadding.Top));
                    IEnumerable<UIElement> topElements = VisualTreeHelper.FindElementsInHostCoordinates(
                        topPoint, this.scrollViewer);
                    ReorderListBoxItem topItem = topElements.OfType<ReorderListBoxItem>().FirstOrDefault();
                    if (topItem != null)
                    {
                        GeneralTransform itemTransform = topItem.TransformToVisual(Application.Current.RootVisual);
                        Rect itemRect = itemTransform.TransformBounds(new Rect(new Point(0, 0), topItem.RenderSize));

                        firstIndex = this.ItemContainerGenerator.IndexFromContainer(topItem);
                        if (!includePartial && firstIndex < this.Items.Count - 1 &&
                            itemRect.Top < scrollViewerRect.Top && itemRect.Bottom < scrollViewerRect.Bottom)
                        {
                            firstIndex++;
                        }
                    }

                    Point bottomPoint = ReorderListBox.GetHostCoordinates(new Point(
                        scrollViewerRect.Left + scrollViewerPadding.Left,
                        scrollViewerRect.Bottom - scrollViewerPadding.Bottom - 1));
                    IEnumerable<UIElement> bottomElements = VisualTreeHelper.FindElementsInHostCoordinates(
                        bottomPoint, this.scrollViewer);
                    ReorderListBoxItem bottomItem = bottomElements.OfType<ReorderListBoxItem>().FirstOrDefault();
                    if (bottomItem != null)
                    {
                        GeneralTransform itemTransform = bottomItem.TransformToVisual(Application.Current.RootVisual);
                        Rect itemRect = itemTransform.TransformBounds(
                            new Rect(new Point(0, 0), bottomItem.RenderSize));

                        lastIndex = this.ItemContainerGenerator.IndexFromContainer(bottomItem);
                        if (!includePartial && lastIndex > firstIndex &&
                            itemRect.Bottom > scrollViewerRect.Bottom && itemRect.Top > scrollViewerRect.Top)
                        {
                            lastIndex--;
                        }
                    }
                }
            }
            else
            {
                firstIndex = -1;
                lastIndex = -1;
            }
        }