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;
}
}