virtualizingstackpanel在决定对可见项目周围的项目进行可视化时会考虑哪些因素?
例如:有一个列表视图,当查看第7项时,项目6&虽然没有看到它们,但它们也会被可视化。
如何限制可视化项目,以便只显示可见项目?
答案 0 :(得分:0)
虚拟化是一个非常复杂的主题,但我有一本书很好地描述了它。此外,本书还介绍了如何实现自定义虚拟化,您可能需要实现自定义虚拟化才能实现目标。幸运的是,我发现有人在网上发布了这本书的PDF,你可以点击this link找到它。
虚拟化部分从第129页开始,但非常值得阅读其余内容,因为它中有一些非常有趣的内容。
答案 1 :(得分:0)
我通过覆盖MeasureOverride
函数解决了这个问题,我们已经购买了Sheridan在他的回答中提到的那本书,这取决于虚拟化章节,这是我对我的课程所做的扩展VirtualizingStackPanel
:它有效!
private ItemsControl ItemsOwner { get; set; }
private int StartIndex { get; set; }
private int EndIndex { get; set; }
protected override void OnInitialized(EventArgs e)
{
ItemsOwner = ItemsControl.GetItemsOwner(this) as ItemsControl;
}
protected override Size MeasureOverride(Size availableSize)
{
ItemsControl itemsControl = ItemsControl.GetItemsOwner(this);
// we can set StartIndex& EndIndex to mimic
// VirtualizingStackPanel.CacheSize in .Net 4.5
// for my problem, I just fixed them at the index of item to be shown
StartIndex = PagesView.Instance.SelectedIndex;
EndIndex = StartIndex;
// Virtualize items
IItemContainerGenerator generator = ItemsOwner.ItemContainerGenerator;
GeneratorPosition startPos = generator.GeneratorPositionFromIndex(StartIndex);
int childIndex = startPos.Offset == 0 ? startPos.Index : startPos.Index + 1;
using (generator.StartAt(startPos, GeneratorDirection.Forward, true))
{
for (int i = StartIndex; i <= EndIndex; i++, childIndex++)
{
bool isNewlyRealized;
UIElement child = generator.GenerateNext(out isNewlyRealized) as UIElement;
if (isNewlyRealized)
{
if (childIndex >= InternalChildren.Count)
{
AddInternalChild(child);
}
else
{
InsertInternalChild(childIndex, child);
}
generator.PrepareItemContainer(child);
}
}
}
//DumpGeneratorContent();
// Measure
foreach (UIElement child in InternalChildren)
{
child.Measure(availableSize);
}
// Clean up
CleanupItems();
return availableSize;
}
private void CleanupItems()
{
IItemContainerGenerator generator = ItemsOwner.ItemContainerGenerator;
for (int i = InternalChildren.Count - 1; i >= 0; i--)
{
GeneratorPosition position = new GeneratorPosition(i, 0);
int itemIndex = generator.IndexFromGeneratorPosition(position);
if (itemIndex < StartIndex || itemIndex > EndIndex)
{
generator.Remove(position, 1);
RemoveInternalChildRange(i, 1);
}
}
}