我使用 aListView
来显示在我的应用程序中出现的错误列表。它的行为和外观与 Visual Studio 中的错误列表完全相同。我想在选择最后一个错误项时添加自动滚动(例如,当您将插入符号放在末尾时,Visual Studio 的日志窗口如何自动滚动)。
错误列表在一个 中ObservableCollection
,它被传递给ListView.ItemsSource
这样的:
public ObservableCollection<ErrorListItem> Items;
...
MyListView.ItemsSource = _Items;
我尝试在事件处理程序中执行自动滚动_Items_CollectionChanged
,但因为这是事件ItemsSource
而不是实际事件ListViewItems
,所以很难确定是否选择了最后一项、选择新行等。因为这尤其困难似乎ListViewItems
不是立即创建的。我设法通过延迟设置最后一个项目的调用来使其自动滚动,如下所示:
void _Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// determine the last item to select from 'e'
...
_ItemPendingToBeScrolled = newItemToSelect;
ListView.SelectedItem = newItemToSelect;
Dispatcher.BeginInvoke(DispatcherPriority.Background,
(ThreadStart)delegate
{
if (_ItemPendingToBeScrolled != null)
{
ListView.ScrollIntoView(_ItemPendingToBeScrolled);
ItemPendingToBeScrolled = null;
}
})
}
但这显然不是正确的做法。ListViewItem
另外,如果列表被过滤,我希望事情继续工作(不检查我的源代码中的最后一个项目,但检查ListView
.
有没有办法在将 aListViewItem
添加到ListView
以下添加到绑定集合中时监听事件?这将是为了正确进行自动滚动而捕获的理想事件。还是我可以使用另一种技术?