question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

For Avalonia.Controls.ListBox for Items Property, Support for IList<T> without non-generic IList

See original GitHub issue

Describe the bug For a DataContext View-Model that has Specialized Collections, collections may implement the IList<T> or IReadOnlyList<T> interfaces, neither of which implements non-generic IList interface.

Any such collections when they also implement INotifyCollectionChanged cause assignment of the DataContext involved throw an exception stating, correctly, that the collection in question does not implement IList, which is correct - it does not.

Thrown here

My expectations were that ListBoxes would work with generic collections, not only collections that implement IList

Related PR

To Reproduce Steps to reproduce the behavior:

1.) Create a ViewModel that implements INPC, create a collection that implements IList<T> or IReadOnlyList<T> and INotifyCollectionChanged but does not implement IList 2.) Assign the collection as the DataContext of a Window or view such that you have a ListBox that has its Items bound to the collection. 3.) Observe the argument exception in question.

Expected behavior The expectation is that ListBox classes should be able to display the items of both IList<T> and IReadOnlyList<T> and update their views as the collections change so long as they implement INotifyCollectionChanged

Screenshots Not really relevant to this.

Desktop (please complete the following information):

Also not really relevant to this either.

Additional context Could ItemsSourceView be modified around Line 49 to check before throwing an exception if Source is IReadOnlyList<object>? In my testing _qc2 is IReadOnlyList<object> returned true for instances of a class definition internal class QuickCollectionWithoutIList : IReadOnlyList<QuickModel>, INotifyCollectionChanged

But that’s incompatible with the non-generic IList interface and can’t be cast to IList properly. What a pain! And the Inner property of ItemsSourceView needs filling.

I feel like this is perhaps stepping onto an earlier pain-point of the .Net BCL similar to the generic IEnumerable<T> and the non-generic IEnumerable. 🙃

I suppose you could if you wanted to you could do some interesting castings, but I don’t know if there are any IList implementing classes that don’t also implement IReadOnlyList<T> - and all IReadOnlyList<T> are castable to IReadOnlyList<object> because of the rules of Covariance. But after checking the IList documentation there’s plenty of types out there that implement IList but don’t implement IReadOnlyList<T> - what a pain!

Would it be too much to have some sort of interface of an indexing provider, that could have implementations that use either IList as a parameter, or IReadOnlyList<T>, and all it needs to do is provide a method of indexing the lists?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
TomEdwardsEnscapecommented, Aug 22, 2022

A user may want to display the contents of a simple enumerable and then refresh the UI manually when they know/judge that it has changed. But since a copy was taken, this won’t work.

The case of an infinite enumerable is a pain, though you could throw an exception after enumerating int32.MaxValue times. I suspect that the List<T> constructors currently in use do this already.

0reactions
timuniecommented, Aug 22, 2022

I don’t think IEnumerable can be cast to IList in any case. The enumeration may be infinite, while IList may not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ListBox
The ListBox is an ItemsControl which displays items in a multi-line list box and allows individual selection.
Read more >
ListBox Divider according to Item Property
You can create a collection view over your collection and tell it to group by your Group property. You can then define a...
Read more >
Data binding in depth - UWP applications
Binding to generic IList(Of T) and IEnumerable(Of T) is not supported. Implement IVector of IInspectable. See XAML items controls; bind to a C ......
Read more >
ListBox Item Height : r/AvaloniaUI
I have a simple ListBox with a few ListBoxItems. I would like to set the height of the items to say 20 pixels...
Read more >
Multiplatform UI Coding with AvaloniaUI in Easy Samples. ...
Avalonia has Visual Studio and Rider support for creating XAML ... The best way to use the list box is by binding its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found