|
|
|
@ -114,7 +114,6 @@ namespace Avalonia.Controls |
|
|
|
/// </summary>
|
|
|
|
static ContextMenu() |
|
|
|
{ |
|
|
|
ItemsPanelProperty.OverrideDefaultValue<ContextMenu>(DefaultPanel); |
|
|
|
PlacementProperty.OverrideDefaultValue<ContextMenu>(PlacementMode.Pointer); |
|
|
|
ContextMenuProperty.Changed.Subscribe(ContextMenuChanged); |
|
|
|
AutomationProperties.AccessibilityViewProperty.OverrideDefaultValue<ContextMenu>(AccessibilityView.Control); |
|
|
|
@ -216,18 +215,23 @@ namespace Avalonia.Controls |
|
|
|
if (e.OldValue is ContextMenu oldMenu) |
|
|
|
{ |
|
|
|
control.ContextRequested -= ControlContextRequested; |
|
|
|
control.AttachedToVisualTree -= ControlOnAttachedToVisualTree; |
|
|
|
control.DetachedFromVisualTree -= ControlDetachedFromVisualTree; |
|
|
|
oldMenu._attachedControls?.Remove(control); |
|
|
|
((ISetLogicalParent?)oldMenu._popup)?.SetParent(null); |
|
|
|
} |
|
|
|
|
|
|
|
if (e.NewValue is ContextMenu newMenu) |
|
|
|
if (e.NewValue is ContextMenu) |
|
|
|
{ |
|
|
|
newMenu._attachedControls ??= new List<Control>(); |
|
|
|
newMenu._attachedControls.Add(control); |
|
|
|
control.ContextRequested += ControlContextRequested; |
|
|
|
control.AttachedToVisualTree += ControlOnAttachedToVisualTree; |
|
|
|
control.DetachedFromVisualTree += ControlDetachedFromVisualTree; |
|
|
|
} |
|
|
|
|
|
|
|
if (control.IsAttachedToVisualTree) |
|
|
|
{ |
|
|
|
AttachControlToContextMenu(control); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
|
|
|
@ -428,11 +432,25 @@ namespace Avalonia.Controls |
|
|
|
e.Handled = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void ControlOnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) |
|
|
|
{ |
|
|
|
AttachControlToContextMenu(sender); |
|
|
|
} |
|
|
|
|
|
|
|
private static void AttachControlToContextMenu(object? sender) |
|
|
|
{ |
|
|
|
if (sender is Control { ContextMenu: { } contextMenu } control) |
|
|
|
{ |
|
|
|
contextMenu._attachedControls ??= new List<Control>(); |
|
|
|
contextMenu._attachedControls.Add(control); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static void ControlDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) |
|
|
|
{ |
|
|
|
if (sender is Control control |
|
|
|
&& control.ContextMenu is ContextMenu contextMenu) |
|
|
|
if (sender is Control { ContextMenu: { } contextMenu } control) |
|
|
|
{ |
|
|
|
if (contextMenu._popup?.Parent == control) |
|
|
|
{ |
|
|
|
@ -440,6 +458,7 @@ namespace Avalonia.Controls |
|
|
|
} |
|
|
|
|
|
|
|
contextMenu.Close(); |
|
|
|
contextMenu._attachedControls?.Remove(control); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|