Change Notifications

Property Changes

In order for Avalonia to know when a property on a view model has changed, the view model must implement change notifications. The easiest way to do this is to use ReactiveUI and make your view model class inherit from ReactiveObject.
You then add a setter for each property which calls RaiseAndSetIfChanged:
1
using ReactiveUI;
2
​
3
public class MyViewModel : ReactiveObject
4
{
5
private string caption;
6
​
7
public string Caption
8
{
9
get => caption;
10
set => this.RaiseAndSetIfChanged(ref caption, value);
11
}
12
}
Copied!
For more information see the ReactiveUI documentation.
If you don't want a dependency on ReactiveUI, you can implement INotifyPropertyChanged manually.

Collection Changes

Collections also need to implement change notifications. There are a number of collection which do this for you out of the box:
    ​ObservableCollection is available in the .NET base class library
    ​DynamicData for more advanced scenarios
    ​ReactiveList in ReactiveUI (deprecated in favor of DynamicData)
    AvaloniaList is shipped with Avalonia, but note that its API may change in future so for the moment it's recommended to not use it at the application level
If you want to implement collection change notifications yourself you can implement INotifyCollectionChanged​
Last modified 6mo ago
Export as PDF
Copy link