WPF ListBox Scroll to end automatically

后端 未结 9 791
耶瑟儿~
耶瑟儿~ 2020-12-13 08:25

In my application, I have a ListBox with items. The application is written in WPF.

How can I scroll automatically to the last added item? I want the

9条回答
  •  再見小時候
    2020-12-13 09:07

    The easiest way to do this:

    if (VisualTreeHelper.GetChildrenCount(listView) > 0)
    {
        Border border = (Border)VisualTreeHelper.GetChild(listView, 0);
        ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
        scrollViewer.ScrollToBottom();
    }
    

    It is always working for ListView and ListBox controls. Attach this code to the listView.Items.SourceCollection.CollectionChanged event and you have fully automatic auto-scrolling behaviour.

提交回复
热议问题