If I have a virtualizingstackpanel inside of my scrollviewer then how does my scrollviewer know what height the scrollbar needs to be?

For example if I have 10,000 items how does it know that I have only scrolled 5 percent down the list?

What if I do not know the size of the items?

有帮助吗?

解决方案

this is a really good resource, at the bottom you can download a sample project. I think what you are looking for is in the GetFirstVisibleIndex and GetLastVisibleIndex functions. I've listed them below for convenience. Basically you'll have an offset tracking how far the 'top' of your list is. that combined with the size of the items will help get to the values you are looking for

 private int GetFirstVisibleIndex()
 {
    return (int)Math.Floor(_offset.Y / _itemSize.Height);
 }

 private int GetLastVisibleIndex()
 {
    return Math.Min((int)Math.Ceiling((_offset.Y + _viewport.Height) / _itemSize.Height) - 1, GetItemsCount() - 1);
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top