Browse Source

Fixed children being attached to the visual or logical tree multiple times

pull/10803/head
Tom Edwards 3 years ago
parent
commit
b400a1f72d
  1. 2
      src/Avalonia.Base/StyledElement.cs
  2. 2
      src/Avalonia.Base/Visual.cs
  3. 6
      tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

2
src/Avalonia.Base/StyledElement.cs

@ -891,7 +891,7 @@ namespace Avalonia
for (var i = 0; i < logicalChildrenCount; i++)
{
if (logicalChildren[i] is StyledElement child)
if (logicalChildren[i] is StyledElement child && child._logicalRoot != e.Root) // child may already have been attached within an event handler
{
child.OnAttachedToLogicalTreeCore(e);
}

2
src/Avalonia.Base/Visual.cs

@ -487,7 +487,7 @@ namespace Avalonia
for (var i = 0; i < visualChildrenCount; i++)
{
if (visualChildren[i] is { } child)
if (visualChildren[i] is { } child && child._visualRoot != e.Root) // child may already have been attached within an event handler
{
child.OnAttachedToVisualTreeCore(e);
}

6
tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

@ -250,13 +250,13 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(0, canExecuteCallCount);
flyout.ShowAt(button);
Assert.Equal(2, canExecuteCallCount);
Assert.Equal(1, canExecuteCallCount);
command.RaiseCanExecuteChanged();
Assert.Equal(3, canExecuteCallCount);
Assert.Equal(2, canExecuteCallCount);
target.CommandParameter = true;
Assert.Equal(4, canExecuteCallCount);
Assert.Equal(3, canExecuteCallCount);
}
}

Loading…
Cancel
Save