简体   繁体   English

从列表框的ScrollInfo获取有效值

[英]Getting valid values from ScrollInfo of a ListBox

I have a ListBox, the issue I'm facing is trying to get valid values from the IScrollInfo object. 我有一个ListBox,我面临的问题是试图从IScrollInfo对象获取有效值。

I know from setting MaxHeight on the ListBox to 150 that the ViewportHeight should be around this size and the full height is twice as big so the ExtentHeight should be about 300. 通过将ListBox的MaxHeight设置为150,我知道ViewportHeight应该在这个大小左右,并且整个高度是它的两倍,因此ExtentHeight应该是300。

I get the following values from: 我从中获得以下值:
_scrollInfo.ExtentHeight = 13
_scrollInfo.ViewportHeight = 7
_scrollInfo.VerticalOffset = varying but 1-6

The values from: 来自的值:
UIElement scrollable = _scrollInfo as UIElement;
seem to be correct. 似乎是正确的。
scrollable.RenderSize.Height = 146 which is roughly right. scrollable.RenderSize.Height = 146大致正确。

What I'm wondering about is: 我想知道的是:

When my ListBox control is first loaded it is bound to an empty ObservableCollection . 当我的ListBox控件第一次加载时,它绑定到一个空的ObservableCollection It is not until later that items are added. 直到后来才添加项目。 Could it be that the IScrollInfo object is retaining these initial values from when the ListBox was empty? 可能是IScrollInfo对象保留了ListBox为空时的这些初始值吗?

The other thing the IScrollInfo object is a VirtualizingStackPanel could this have any bearing on the matter? IScrollInfo对象的另一件事是VirtualizingStackPanel ,这对事情有影响吗?

[EDIT] [编辑]
Have tried changing the VirtualizingStackPanel to StackPanel , however I am still getting the same results. 尝试将VirtualizingStackPanel更改为StackPanel ,但是我仍然得到相同的结果。

This behaviour is given by ScrollViewer.CanContentScroll="True" It basically says, that you are not allowed to scroll by pixels, but only by items. 此行为由ScrollViewer.CanContentScroll="True"给出。它基本上说,不允许按像素滚动,而只能按项目滚动。

Consider this example: 考虑以下示例:

private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    var scrollViewer = (ScrollViewer) sender;
    Trace.WriteLine(string.Format("ExtentHeight: {0}, ViewportHeight : {1}, VerticalOffset : {2}",
        scrollViewer.ExtentHeight, scrollViewer.ViewportHeight, scrollViewer.VerticalOffset));
}


<ScrollViewer Height="150" ScrollChanged="ScrollViewer_ScrollChanged" CanContentScroll="True">
    <VirtualizingStackPanel>
        <Rectangle Height="40" Margin="5" Fill="Red" />
        <Rectangle Height="40" Margin="5" Fill="Green" />
        <Rectangle Height="40" Margin="5" Fill="Blue" />
        <Rectangle Height="40" Margin="5" Fill="Red" />
        <Rectangle Height="40" Margin="5" Fill="Green" />
        <Rectangle Height="40" Margin="5" Fill="Blue" />
        <Rectangle Height="40" Margin="5" Fill="Red" />
    </VirtualizingStackPanel>
</ScrollViewer>

you get following output: 您得到以下输出:

ExtentHeight: 7, ViewportHeight : 3, VerticalOffset : 0   
ExtentHeight: 7, ViewportHeight : 3, VerticalOffset : 1
ExtentHeight: 7, ViewportHeight : 3, VerticalOffset : 2   

but when you set CanContentScroll="False" : 但是当您设置CanContentScroll="False"

ExtentHeight: 350, ViewportHeight : 150, VerticalOffset : 3,01724137931035
ExtentHeight: 350, ViewportHeight : 150, VerticalOffset : 6,03448275862069
ExtentHeight: 350, ViewportHeight : 150, VerticalOffset : 9,05172413793104
ExtentHeight: 350, ViewportHeight : 150, VerticalOffset : 12,0689655172414
ExtentHeight: 350, ViewportHeight : 150, VerticalOffset : 15,0862068965517
ExtentHeight: 350, ViewportHeight : 150, VerticalOffset : 18,1034482758621
ExtentHeight: 350, ViewportHeight : 150, VerticalOffset : 21,1206896551724

In the first example you are scrolling by items. 在第一个示例中,您将按项目滚动。 You have seven items, so ExtentHeight is 7 and 3 items are visible, so ViewportHeight is 3. 您有七个项目,因此ExtentHeight为7,可见的项目为3,因此ViewportHeight为3。

In the second example you are scrolling by pixels, so ExtentHeight is total height of all items and viewport height is height of scrollviewver 在第二个示例中,您按像素滚动,因此ExtentHeight是所有项目的总高度,视口高度是scrollviewver的高度

Moral of the story is, that in some scenarios you dont want to measure size of all items, because it could have negative performance effect. 故事的寓意是,在某些情况下,您不想度量所有项目的大小,因为它可能会对性能产生负面影响。 It's the case especially when virtualizing elements. 尤其是在虚拟化元素时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM