Observable Collections
Observable collections provide automatic change notification for collections in Avalonia UI, enabling UI elements to automatically update when items are added, removed, or changed.AvaloniaList<T>
Avalonia’s built-in observable list implementation.Namespace
Inheritance
Constructor
Properties
Gets the number of items in the collection.
Gets or sets the item at the specified index.
Methods
Adds an item to the collection.Parameters:
item(T): The item to add
Adds multiple items to the collection in a single operation.Parameters:
items(IEnumerable<T>): The items to add
Removes an item from the collection.Parameters:
item(T): The item to remove
true if the item was removedRemoves the item at the specified index.Parameters:
index(int): The index of the item to remove
Removes all items from the collection.
Inserts an item at the specified index.Parameters:
index(int): The index to insert atitem(T): The item to insert
Moves an item from one index to another.Parameters:
oldIndex(int): The current indexnewIndex(int): The new index
Events
Occurs when the collection changes (items added, removed, moved, or reset).
Occurs when a property value changes (typically Count or Item[]).
Usage Examples
Basic ViewModel with ObservableCollection
Binding to ListBox
Adding Multiple Items
Collection Change Notification
System.Collections.ObjectModel.ObservableCollection<T>
You can also use the standard .NETObservableCollection\<T\> which is fully supported:
Both
AvaloniaList\<T\> and ObservableCollection\<T\> work with Avalonia’s data binding system. AvaloniaList\<T\> provides additional methods like AddRange for batch operations.Filtered and Sorted Collections
Using DataGridCollectionView (for filtering/sorting)
Performance Tips
Use AddRange for multiple items
Use AddRange for multiple items
When adding multiple items, use
AddRange instead of calling Add multiple times to reduce change notifications.Use virtualization for large collections
Use virtualization for large collections
For large collections in ListBox or similar controls, enable virtualization:
Implement INotifyPropertyChanged on items
Implement INotifyPropertyChanged on items
For the UI to update when item properties change, your item class must implement
INotifyPropertyChanged or use ObservableObject from CommunityToolkit.Mvvm.