Consolidated Scrolling - “Pixel by Pixel” + “Item

2019-02-28 05:06发布

问题:

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 then it gets messed up.
As the logic of scrolling is based on Viewport height and as per MSDN we've -

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.

So scrolling dint work i want something like--

ScrollToContent(ScrollViewer, CurrentMousePositionWRTScrollViewer)
{ 
   if(ScrollViewer's Viewport Height is in terms of Pixel)
{
----------Do Pixel by Pixel Scrolling --------
}
else if(ScrollViewer's Viewport Height represents number of items visible)
{ 
--------- Do Item by Item Scrolling ---------
}

I tried putting check of "CanContentScroll = false", checking Virtualization is enabled or not but even that dint work because i found that in one even if CanContentScroll is true Viewport height dint represent the number of items visible but it's equal to actual height. However on another listbox it shows - the number of items visible.

Actual Code of scrolling -

private void ScrollToContent(ScrollViewer scrollViewer, Point point)
    {
        double verticalScrollOffset = 0.0;
        double scrollDifference = 30.0;
        double scrollDefaultOffset = 40.0;

        if (scrollViewer == null) return;
        if (scrollViewer.ViewportHeight != scrollViewer.ExtentHeight)
        {
                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);
                }
        }
    }

I was in illusion that virtualization is culprit but after checking IsVirtualization property, I noticed that virtualization is not an issue here (its true for both listboxes) .. Any Idea what can be the possible case ??
Issue is - I've 2 listboxes (almost similar) In one case i get ViewPort Height == Number of Items visible However in other case ViewPort Height = Actual Height ..

What can be the possible reason ??

回答1:

bool isVirtualizing = (bool)scrollViewer.GetValue(VirtualizingStackPanel.IsVirtualizingProperty);
VirtualizationMode mode = (VirtualizationMode)scrollViewer.GetValue(VirtualizingStackPanel.VirtualizationModeProperty);


回答2:

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