Browse Source

Don't modify logical tree in ItemsSource mode.

pull/10590/head
Steven Kirk 4 years ago
parent
commit
9856711494
  1. 17
      src/Avalonia.Controls/ItemsControl.cs
  2. 31
      tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs

17
src/Avalonia.Controls/ItemsControl.cs

@ -573,15 +573,18 @@ namespace Avalonia.Controls
/// <param name="e">The event args.</param>
private protected virtual void OnItemsViewCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
if (!_items.IsReadOnly)
{
case NotifyCollectionChangedAction.Add:
AddControlItemsToLogicalChildren(e.NewItems);
break;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
AddControlItemsToLogicalChildren(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
RemoveControlItemsFromLogicalChildren(e.OldItems);
break;
case NotifyCollectionChangedAction.Remove:
RemoveControlItemsFromLogicalChildren(e.OldItems);
break;
}
}
ItemCount = ItemsView.Count;

31
tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs

@ -249,6 +249,37 @@ namespace Avalonia.Controls.UnitTests
Assert.Null(((ILogical)child).LogicalParent);
}
[Fact]
public void Assigning_ItemsSource_Should_Not_Fire_LogicalChildren_CollectionChanged_Before_ApplyTemplate()
{
var target = new ItemsControl();
var child = new Control();
var called = false;
((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
var list = new AvaloniaList<Control>(new[] { child });
target.ItemsSource = list;
Assert.False(called);
}
[Fact]
public void Changing_ItemsSource_Should_Not_Fire_LogicalChildren_CollectionChanged_Before_ApplyTemplate()
{
var target = new ItemsControl();
var child = new Control();
var called = false;
((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
var list = new AvaloniaList<Control>();
target.ItemsSource = list;
list.Add(child);
Assert.False(called);
}
[Fact]
public void Clearing_Items_Should_Clear_Child_Controls_Parent()
{

Loading…
Cancel
Save