WPF ListView 的基础集合更改时如何正确调整滚动条的大小?
我有一个 WPF ListView 绑定到具有数千个项目的可观察集合。当大量这些被删除时,视图似乎只显示最后一项。当我使用拇指栏移动视图中的位置时,拇指栏会调整大小以反映新的集合大小。是否可以在集合更改时强制 ListView 和滚动条同步?
如果其他人有这个问题,我已经找到了解决方法。
下面的代码示例显示了在第一行更改的 ListView 的项目源。以下几行显示了只是滚动回第一项的解决方法。
this.ListViewResults.ItemsSource = this.itemsFiltered;
object firstItem = this.ListViewResults.Items.GetItemAt(0);
if(firstItem == null)
{
return;
}
this.ListViewResults.ScrollIntoView(firstItem);
奇怪的行为!!
我会尝试将 ListView 的绑定上下文(上下文)设置为 null,然后再次设置相同的列表以刷新绑定。
我有一个不同的解决方法,需要子类化 ListView。这需要更多的工作,但结果比仅滚动到第一项要好。但是您需要调整 ListView 模板,以便模板中的 ScrollViewer 有一个名称(此处为 PART_ScrollViewer),或者您使用另一种方式来获取 ScrollViewer 对象。
public class BetterListView : ListView
{
ScrollViewer sv;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
//Get the scrollviewer in the template (I adapted the ListView template such that the ScrollViewer has a name property)
sv = (this.Template.FindName("PART_ScrollViewer", this)) as ScrollViewer;
}
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
//Prevent the bug where the ListView doesn't scroll correctly when a lot of items are removed
if (sv != null && e.Action == NotifyCollectionChangedAction.Remove)
{
sv.InvalidateScrollInfo();
}
}
}