3

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?

Anthony Brien
  • 6,106
  • 7
  • 43
  • 56

1 Answers1

0

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