自动滚动列表视图到最后添加的项目的最佳方法是什么?

发布于 2024-07-18 06:55:53 字数 1448 浏览 6 评论 0原文

我使用 ListView 显示应用程序中发生的错误列表。 它的行为和外观与 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;
            } 
        })
}

但这显然不是正确的方法。 另外,如果列表被过滤,我希望事情继续工作(不检查源中的最后一项,而是检查 ListView 中的最后一个 ListViewItem)。

在添加到绑定集合后,将 ListViewItem 添加到 ListView 时,是否有办法侦听事件? 为了正确地进行自动滚动,这将是捕获的理想事件。 或者我可以使用另一种技术吗?

I use a ListView to show a list of errors as they occur in my application. It behaves and looks exactly like the Error List in Visual Studio. I want to add auto-scrolling when the last error item is selected (like how Visual Studio's Log Window auto-scrolls when you place the caret at the end).

The list of errors is in an ObservableCollection, which is passed to the ListView.ItemsSource like this:

public ObservableCollection<ErrorListItem> Items;
... 
MyListView.ItemsSource = _Items;

I tried performing the auto-scroll in the _Items_CollectionChanged event handler, but because this is the event on the ItemsSource and not on the actual ListViewItems, it's a pain to figure out if the last item is selected, select the new row, etc. It's especially hard since it seems the ListViewItems are not created instantly. I managed to make it auto-scroll by delaying the call to set the last item selected like this:

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;
            } 
        })
}

But that's obviously not the right way to do it. Also, I want things to keep working if the list is filtered (not checking the last item in my source, but the last ListViewItem in the ListView).

Is there a way to listen to events when a ListViewItem gets added to the ListView following an addition to the bound collection? That would be the ideal event to capture in order to properly do my auto-scrolling. Or is there another technique I could use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2024-07-25 06:55:53

我对列表框/列表视图及其滚动有很多问题,但是,您提到挂钩列表视图的更改事件,是因为您无法侦听可观察集合的 CollectionChanged 事件吗? ObservableCollection 比 List 控件稳定得多,并且您会收到相同的通知。

如果这些事件在 UI 中不起作用并且您无权访问,您也可以将这些事件冒泡起来,这样您就可以在 UI 中进行滚动,而无需访问实际的集合,只需在自定义中保留对所选项目的引用即可事件参数类

I have a lot of issues with listboxes/listviews and their scrolling, however, you mentioned hooking to the listview's changed event, is it because you can't listen to the observable collection's CollectionChanged event? ObservableCollection is way more stable than List controls, and you'll get the same notifications.

You can also bubble these events up if it's not working in the UI and you don't have access, this way you treat your scrolling in the UI without having access to the actual collection, just keep a reference to the Selected Item in your custom EventArgs class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文