Browse Source
Merge pull request #4565 from AvaloniaUI/fixes/crashing-context-menu-when-previously-opened-on-control-no-longer-attached-visual-tree
Fixes Context Menu crashing when opened on a second control and the first no longer in visual tree.
pull/4567/head
danwalmsley
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
44 additions and
0 deletions
-
src/Avalonia.Controls/ContextMenu.cs
-
tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs
|
|
|
@ -279,6 +279,11 @@ namespace Avalonia.Controls |
|
|
|
((ISetLogicalParent)_popup).SetParent(control); |
|
|
|
} |
|
|
|
|
|
|
|
if (PlacementTarget is null && _popup.PlacementTarget != control) |
|
|
|
{ |
|
|
|
_popup.PlacementTarget = control; |
|
|
|
} |
|
|
|
|
|
|
|
_popup.Child = this; |
|
|
|
IsOpen = true; |
|
|
|
_popup.IsOpen = true; |
|
|
|
|
|
|
|
@ -1,5 +1,6 @@ |
|
|
|
using System; |
|
|
|
using Avalonia.Input; |
|
|
|
using Avalonia.LogicalTree; |
|
|
|
using Avalonia.Markup.Xaml; |
|
|
|
using Avalonia.Markup.Xaml.MarkupExtensions; |
|
|
|
using Avalonia.Platform; |
|
|
|
@ -132,6 +133,44 @@ namespace Avalonia.Controls.UnitTests |
|
|
|
popupImpl.Verify(x => x.Show(), Times.Exactly(2)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Context_Menu_Can_Be_Shared_Between_Controls_Even_After_A_Control_Is_Removed_From_Visual_Tree() |
|
|
|
{ |
|
|
|
using (Application()) |
|
|
|
{ |
|
|
|
var sut = new ContextMenu(); |
|
|
|
var target1 = new Panel |
|
|
|
{ |
|
|
|
ContextMenu = sut |
|
|
|
}; |
|
|
|
|
|
|
|
var target2 = new Panel |
|
|
|
{ |
|
|
|
ContextMenu = sut |
|
|
|
}; |
|
|
|
|
|
|
|
var sp = new StackPanel { Children = { target1, target2 } }; |
|
|
|
var window = new Window { Content = sp }; |
|
|
|
|
|
|
|
window.ApplyTemplate(); |
|
|
|
window.Presenter.ApplyTemplate(); |
|
|
|
|
|
|
|
_mouse.Click(target1, MouseButton.Right); |
|
|
|
|
|
|
|
Assert.True(sut.IsOpen); |
|
|
|
|
|
|
|
_mouse.Click(target2, MouseButton.Left); |
|
|
|
|
|
|
|
Assert.False(sut.IsOpen); |
|
|
|
|
|
|
|
sp.Children.Remove(target1); |
|
|
|
|
|
|
|
_mouse.Click(target2, MouseButton.Right); |
|
|
|
|
|
|
|
Assert.True(sut.IsOpen); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Cancelling_Opening_Does_Not_Show_ContextMenu() |
|
|
|
|