Skip to content

Cannot figure out how to implement CollectionChanged in custom control #18570

Answered by tobyfirth
loofa asked this question in Q&A
Discussion options

You must be logged in to vote

OnPropertyChanged will be called when Things changes to a different value. As the function is called when an Avalonia property changed. So will only be called when a different collection object is bound to Things.

You should use the OnPropertyChanged function to subscribe to the CollectionChanged event on the collection (make sure you unsubscribe to the event on the previous collection).

It could look something like this.

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
    if (e.Property == ThingsProperty)
    {
        var (oldValue, newValue) = e.GetOldAndNewValue<ObservableCollection<Thing>>();
        oldValue.CollectionChanged -= OnThingsChanged;

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@loofa
Comment options

@tobyfirth
Comment options

@loofa
Comment options

Answer selected by loofa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants