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

FindViewLastIndex() private method

Finds the index of the last visible item by starting at the first index and comparing the bounds of each following item to the ScrollViewer bounds.
This method is less efficient than the hit-test method used by GetViewIndexRange() above, but it works when the controls haven't actually been rendered yet, while the other doesn't.
private FindViewLastIndex ( int firstIndex ) : int
firstIndex int
return int
        private int FindViewLastIndex(int firstIndex)
        {
            int lastIndex = firstIndex;

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

            while (lastIndex < this.Items.Count - 1)
            {
                ReorderListBoxItem itemContainer = (ReorderListBoxItem)
                    this.ItemContainerGenerator.ContainerFromIndex(lastIndex + 1);
                if (itemContainer == null)
                {
                    break;
                }

                GeneralTransform itemTransform = itemContainer.TransformToVisual(
                    Application.Current.RootVisual);
                Rect itemRect = itemTransform.TransformBounds(new Rect(new Point(0, 0), itemContainer.RenderSize));
                itemRect.Intersect(scrollViewerRect);
                if (itemRect == Rect.Empty)
                {
                    break;
                }

                lastIndex++;
            }

            return lastIndex;
        }