microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

ListBox and ListView don't listen for INotifyCollectionChanged if the type implements a generic IList<T> interface; it *must* be the legacy IList.

Open ranjeshj opened this issue 5 years ago • 8 comments

Describe the bug ListBox/ListView require ItemsSource to impliment the legacy IList interface. IList<T> is not supported by WinRT. It should be possible to support cases where the the ItemsSource only impliements IEnumerable and INCC

Internal bug - https://microsoft.visualstudio.com/OS/_queries/edit/24511651

ranjeshj avatar Jan 03 '20 17:01 ranjeshj

Is this related to https://github.com/microsoft/microsoft-ui-xaml/issues/931 then as well?

michael-hawker avatar Jan 16 '20 19:01 michael-hawker

related but solvable differently. I think the issue here is that we want collection controls to be able to work with just IEnumerable + INCC. If WinRT supported generics, then collection controls don't have to depend on the 'IList' interface.

ranjeshj avatar Jan 16 '20 19:01 ranjeshj

Just hit this again, been scratching my head for hours on why my custom collection with just INotifyCollectionChanged wasn't triggering the ListViewBase to update its view of my collection items... ☹

michael-hawker avatar Apr 22 '20 22:04 michael-hawker

@michael-hawker, what list interfaces does your collection implement?

MikeHillberg avatar Apr 23 '20 20:04 MikeHillberg

@MikeHillberg originally it was just IEnumerable<object> and INotifyCollectionChanged, adding IList fixed the problem, but meant I had to add more implementation details (which I may have needed anyway in the future, but was testing out the basics first).

michael-hawker avatar Apr 27 '20 21:04 michael-hawker

And it looks like IList but not IList<T>. IEnumerable or IEnumerable<T> should be enough.

MikeHillberg avatar Apr 28 '20 04:04 MikeHillberg

@MikeHillberg, yeah that's what I was expecting initially, but it would only listen to updates once it was an IList.

The problem I'm running into now is that if I don't raise the right NotifyCollectionChangedEventArgs than ListView tries to add an existing container to itself and I get an unhandled exception with a duplicate item in the tree.

Is this process documented anywhere with examples of what items adding/moving/removing in the collection should correspond to event args and how ListView updates its containers? I also think I'm running into issues with performing multiple operations on the collection and needing to give ListView one event to modify the containers?

michael-hawker avatar Apr 30 '20 03:04 michael-hawker

I agree with @MikeHillberg. NotifyCollectionChangedEventArgs contains not only NewStartingIndex and OldStartingIndex properties, but also NewItems and OldItems lists. This is enough to properly handle all changes by the INCC interface consumer.

I've checked it and currently (AppSDK 1.1.4) uses IList[index] to obtain the item located at NewStartingIndex. So you are out of luck if you try to use something like ConcurrentQueue<T> as a backing item collection.

KubaSzostak avatar Sep 20 '22 18:09 KubaSzostak

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jul 29 '23 13:07 github-actions[bot]