我想出了一个解决方案...我把itemscontrol放到scrollviewer中,而不是ItemContainerStyle中的scrollviewer。
所以基本上我跟踪scrollviewer的虚拟大小和跟踪日志收集。当暂停并且集合发生变化时,将滚动查看器的垂直偏移量移动多少,然后禁用动画,以便移动不可见,因此它明显保持在同一位置。
private void Page_Loaded(object sender, RoutedEventArgs e)
{
Log.Entries.CollectionChanged += Entries_CollectionChanged;
this.LogItemsControl.ItemsSource = Log.Entries;
lastExtentHeight = this.ScrollViewer.ExtentHeight;
}
private void Entries_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
lastExtentHeight = this.ScrollViewer.ExtentHeight;
if (scrollingPaused)
{
double sizeGrown = ScrollViewer.ExtentHeight - pausedExtentHeight;
pausedVerticalOffset += sizeGrown;
pausedExtentHeight = ScrollViewer.ExtentHeight;
this.ScrollViewer.ChangeView(0, pausedVerticalOffset, 1, true);
}
}
private void PlayPauseAppBarButton_Click(object sender, RoutedEventArgs e)
{
if (scrollingPaused)
{
// UnPause
scrollingPaused = false;
}
else
{
// Pause
pausedVerticalOffset = this.ScrollViewer.VerticalOffset;
pausedExtentHeight = lastExtentHeight;
scrollingPaused = true;
}
}
你可以只增加一个间接其中更新被送入一个队列时,列表被暂停的水平。不要更新主列表。当它没有被使用时,只需将队列移动到绑定列表。 – WiredPrairie