Browse Source

Fix issue with MenuItem.Click not called when the item is not embdded into a Menu

pull/5451/head
Luis von der Eltz 5 years ago
parent
commit
f207a6a61e
  1. 28
      src/Avalonia.Controls/MenuItem.cs

28
src/Avalonia.Controls/MenuItem.cs

@ -103,6 +103,7 @@ namespace Avalonia.Controls
private bool _commandCanExecute = true; private bool _commandCanExecute = true;
private Popup? _popup; private Popup? _popup;
private KeyGesture _hotkey; private KeyGesture _hotkey;
private bool _isEmbeddedInMenu;
/// <summary> /// <summary>
/// Initializes static members of the <see cref="MenuItem"/> class. /// Initializes static members of the <see cref="MenuItem"/> class.
@ -147,7 +148,7 @@ namespace Avalonia.Controls
{ {
var parent = x as Control; var parent = x as Control;
return parent?.GetObservable(DefinitionBase.PrivateSharedSizeScopeProperty) ?? return parent?.GetObservable(DefinitionBase.PrivateSharedSizeScopeProperty) ??
Observable.Return<DefinitionBase.SharedSizeScope?>(null); Observable.Return<DefinitionBase.SharedSizeScope?>(null);
}); });
this.Bind(DefinitionBase.PrivateSharedSizeScopeProperty, parentSharedSizeScope); this.Bind(DefinitionBase.PrivateSharedSizeScopeProperty, parentSharedSizeScope);
@ -275,7 +276,7 @@ namespace Avalonia.Controls
public bool IsTopLevel => Parent is Menu; public bool IsTopLevel => Parent is Menu;
/// <inheritdoc/> /// <inheritdoc/>
bool IMenuItem.IsPointerOverSubMenu => _popup?.IsPointerOverPopup ?? false; bool IMenuItem.IsPointerOverSubMenu => _popup?.IsPointerOverPopup ?? false;
/// <inheritdoc/> /// <inheritdoc/>
IMenuElement? IMenuItem.Parent => Parent as IMenuElement; IMenuElement? IMenuItem.Parent => Parent as IMenuElement;
@ -310,7 +311,7 @@ namespace Avalonia.Controls
.Select(x => x.ContainerControl) .Select(x => x.ContainerControl)
.OfType<IMenuItem>(); .OfType<IMenuItem>();
} }
} }
/// <summary> /// <summary>
/// Opens the submenu. /// Opens the submenu.
@ -337,6 +338,18 @@ namespace Avalonia.Controls
return new MenuItemContainerGenerator(this); return new MenuItemContainerGenerator(this);
} }
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
if (!_isEmbeddedInMenu)
{
//Normally the Menu's IMenuInteractionHandler is sending the click events for us
//However when the item is not embedded into a menu we need to send them ourselves.
RaiseEvent(new RoutedEventArgs(ClickEvent));
}
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{ {
if (_hotkey != null) // Control attached again, set Hotkey to create a hotkey manager for this control if (_hotkey != null) // Control attached again, set Hotkey to create a hotkey manager for this control
@ -350,6 +363,15 @@ namespace Avalonia.Controls
{ {
Command.CanExecuteChanged += CanExecuteChanged; Command.CanExecuteChanged += CanExecuteChanged;
} }
var parent = Parent;
while (parent is MenuItem)
{
parent = parent.Parent;
}
_isEmbeddedInMenu = parent is IMenu;
} }
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)

Loading…
Cancel
Save