How to find the index of a newly added item in a ListView collection

Charles Burkitt 30 Reputation points
2023-01-12T15:00:24.92+00:00

I have a ListView element using a GridView. The ListView.ItemsSource property is bound to a CollectionViewSource.

When I add a new item to the collection, the item appears successfully in the grid but I cannot determine the index at which it appears because the CollectionViewSource is sorting the items. Due to the sorting, the order in the grid is not the same as the order in the original collection.

I can access the ListView.Items property, a read-only list of items, which I can search to find my new item and its index, but how do I know when to access this property? If I access it when the CollectionChanged event is raised, that might be too early because the change in the collection might not have propagated through to the ListView items.

There does not seem to be an event raised when the ListView.Items property changes. I tried to bind to this property but was not allowed to bind because the property does not have a set accessor (read-only), even though I was using Mode:OneWayToSource so did not require a set accessor.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Charles Burkitt 30 Reputation points
    2023-01-14T16:16:00.73+00:00

    I think I have found a way to solve my problem without needing the index.

    I have a list of names in a left-hand pane and when a name is selected, details for that name are displayed in a right-hand pane. When I add a new name to the left-hand pane I would like the newly-added person to be selected automatically, with the details showing on the right, but this is not happening. The selector has no reason to select the newly-added person after the collection is updated.

    I was trying to find the index of the new item, then set the SelectedIndex property. However, I have since learned of the SelectedValuePath and SelectedValue properties. On testing, I realise I can bind the SelectedValue to my ViewModel and set it from there after adding the new item. The selector searches for the item with my requested value and selects it. Perfect.

    I suppose I could use the SelectedItem property in a similar way, but that would involve searching for a KeyValuePair object (the item) rather than a mere string (the item Key property) which I thought was overkill.

    Thanks for all help offered.

    1 person found this answer helpful.

  2. Michael Taylor 48,826 Reputation points
    2023-01-12T16:14:00.1433333+00:00

    Why do you need the index? I wonder if you have a solution looking for a problem here. The only reason I can think of why you might need to have the index is so you can do some post-add logic but I would lean toward that being better handled using data binding. For example if you wanted to show an indicator next to new items then perhaps that is better handled by adding a "new" property to the model you're binding to and set that property on new items being added. Then style based upon the property. You need to determine when to reset a new item though.