Skip to content

Commit

Permalink
Merge pull request #6732 from AvaloniaUI/fixes/6729-itemsrepeater-rea…
Browse files Browse the repository at this point in the history
…ssign-items

ItemsRepeater: unsubscribe from ItemsSourceView before disposing it.
  • Loading branch information
maxkatz6 authored and danwalmsley committed Oct 21, 2021
1 parent af00eff commit 776f3ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Avalonia.Controls/Repeater/ItemsRepeater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,14 +588,14 @@ private void OnDataSourcePropertyChanged(ItemsSourceView oldValue, ItemsSourceVi
throw new AvaloniaInternalException("Cannot set ItemsSourceView during layout.");
}

ItemsSourceView?.Dispose();
ItemsSourceView = newValue;

if (oldValue != null)
{
oldValue.CollectionChanged -= OnItemsSourceViewChanged;
}

ItemsSourceView?.Dispose();
ItemsSourceView = newValue;

if (newValue != null)
{
newValue.CollectionChanged += OnItemsSourceViewChanged;
Expand Down
24 changes: 24 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ItemsRepeaterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.ObjectModel;
using Xunit;

namespace Avalonia.Controls.UnitTests
{
public class ItemsRepeaterTests
{
[Fact]
public void Can_Reassign_Items()
{
var target = new ItemsRepeater();
target.Items = new ObservableCollection<string>();
target.Items = new ObservableCollection<string>();
}

[Fact]
public void Can_Reassign_Items_To_Null()
{
var target = new ItemsRepeater();
target.Items = new ObservableCollection<string>();
target.Items = null;
}
}
}

0 comments on commit 776f3ea

Please sign in to comment.