合并滚动-“逐像素” + “逐项”

发布于 2024-11-03 02:36:09 字数 2541 浏览 4 评论 0原文

我有一个拖放框架,其中已经存在滚动支持(逐像素滚动)。在未启用虚拟化的情况下,这很有效,但如果启用了虚拟化,那么它就会变得混乱。
由于滚动的逻辑基于视口高度,并且根据 MSDN,我们 -

MSDN - 如果 CanContentScroll 为 true,则 ExtentHeight、ScrollableHeight、ViewportHeight 和 VerticalOffset 属性的值是项目数。如果 CanContentScroll 为 false,则这些属性的值为设备独立像素。另外,如果启用虚拟化,则 ExtentHeight 表示 - ScrollViewer 中的项目总数,视口高度表示当前可见的项目数。

因此,滚动功能工作我想要类似的东西 -

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

我尝试检查“CanContentScroll = false”,检查虚拟化是否启用,但即使该功能也有效,因为我发现即使 CanContentScroll 为 true,视口高度功能也代表了项目可见,但等于实际高度。然而,在另一个列表框中,它显示 - 可见项目的数量。

滚动的实际代码 -

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

我幻想虚拟化是罪魁祸首,但在检查 IsVirtualization 属性后,我注意到虚拟化在这里不是问题(这对于两个列表框都是如此)..任何想法可能是什么情况??
问题是 - 我有 2 个列表框(几乎相似) 在一种情况下我得到 视口高度 == 可见项目数 然而在其他情况下 视口高度 = 实际高度 ..

可能的原因是什么?

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 ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花间憩 2024-11-10 02:36:09
bool isVirtualizing = (bool)scrollViewer.GetValue(VirtualizingStackPanel.IsVirtualizingProperty);
VirtualizationMode mode = (VirtualizationMode)scrollViewer.GetValue(VirtualizingStackPanel.VirtualizationModeProperty);
bool isVirtualizing = (bool)scrollViewer.GetValue(VirtualizingStackPanel.IsVirtualizingProperty);
VirtualizationMode mode = (VirtualizationMode)scrollViewer.GetValue(VirtualizingStackPanel.VirtualizationModeProperty);
寻找我们的幸福 2024-11-10 02:36:09

回答实际高度与视口高度和滚动问题

代码:我使用选定的项目在启用虚拟化的情况下执行逐项滚动

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

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;
                            }
                        }
                    }
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文