I\'ve a Drag-Drop framework in which scrolling support is already there (Pixel by Pixel scrolling). This works good in case Virtualization is not enabled but if it is enabled th
Answer Actual Height Vs Viewport Height and Scrolling Issue
Code : I used selected Item to perfrom Item by Item scrolling in case of Virtualization enabled
// Manage vertical scrolling.
if (scrollViewer.ViewportHeight != scrollViewer.ExtentHeight)
{
// NOTE :MSDN- If CanContentScroll is true, the values of the ExtentHeight, ScrollableHeight, ViewportHeight, and VerticalOffset
// properties are number of items. If CanContentScroll is false, the values of these properties are Device Independent Pixels.
// Also If Virtualization is Enabled then ExtentHeight Represents - TotalNumber of Items in ScrollViewer and Viewport Height
// Represents Count of Items currently visible.
if (scrollViewer.CanContentScroll == false)
{
if (scrollViewer.ViewportHeight - point.Y < scrollDifference)
{
// See if we need to scroll down
verticalScrollOffset = scrollDefaultOffset;
}
else if (point.Y < scrollDifference)
{
// See if we need to scroll up
verticalScrollOffset = -scrollDefaultOffset;
}
// Scroll up or down
if (verticalScrollOffset != 0.0)
{
verticalScrollOffset += scrollViewer.VerticalOffset;
if (verticalScrollOffset < 0.0)
{
verticalScrollOffset = 0.0;
}
else if (verticalScrollOffset > scrollViewer.ScrollableHeight)
{
verticalScrollOffset = scrollViewer.ScrollableHeight;
}
scrollViewer.ScrollToVerticalOffset(verticalScrollOffset);
}
}
else
{
if (scrollViewer != null)
{
// To ControlScrolling Speed - as we're doing Item By Item Scrolling now, it would be quite fast so add a time gap.
if (DateTime.Now.Subtract(lastScrollTime).TotalMilliseconds > 200)
{
if (scrollViewer.ActualHeight - point.Y < scrollDifference)
{
// See if we need to scroll down
verticalScrollOffset = scrollDefaultOffset;
}
else if (point.Y < scrollDifference)
{
// See if we need to scroll up
verticalScrollOffset = -scrollDefaultOffset;
}
if (verticalScrollOffset != 0.0)
{
// Scroll Content upwards
if (verticalScrollOffset < 0 && scrollViewer.VerticalOffset > 0) // Also check if there are any non visible elements in scrollviewer
{
scrollViewer.LineUp();
lastScrollTime = DateTime.Now;
}
else if (verticalScrollOffset > 0 &&
scrollViewer.VerticalOffset < scrollViewer.ExtentHeight - scrollViewer.ViewportHeight)
{
scrollViewer.LineDown();
lastScrollTime = DateTime.Now;
}
}
}
}
}
}
bool isVirtualizing = (bool)scrollViewer.GetValue(VirtualizingStackPanel.IsVirtualizingProperty);
VirtualizationMode mode = (VirtualizationMode)scrollViewer.GetValue(VirtualizingStackPanel.VirtualizationModeProperty);