Table of Contents

Class MenuItem

Namespace
Avalonia.Controls
Assembly
Avalonia.Controls.dll

A menu item control.

[TemplatePart("PART_Popup", typeof(Popup))]
[PseudoClasses(new string[] { ":separator", ":radio", ":toggle", ":checked", ":icon", ":open", ":pressed", ":selected" })]
public class MenuItem : HeaderedSelectingItemsControl, INotifyPropertyChanged, IDataContextProvider, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed, IDataTemplateHost, ISetterValue, IChildIndexProvider, IInputElement, ISelectable, ICommandSource, ILogical
Inheritance
MenuItem
Implements
Inherited Members
Extension Methods

Constructors

MenuItem()

public MenuItem()

Fields

ClickEvent

Defines the Click event.

public static readonly RoutedEvent<RoutedEventArgs> ClickEvent

Field Value

RoutedEvent<RoutedEventArgs>

CommandParameterProperty

Defines the CommandParameter property.

public static readonly StyledProperty<object?> CommandParameterProperty

Field Value

StyledProperty<object>

CommandProperty

Defines the Command property.

public static readonly StyledProperty<ICommand?> CommandProperty

Field Value

StyledProperty<ICommand>

GroupNameProperty

Defines the GroupName property.

public static readonly StyledProperty<string?> GroupNameProperty

Field Value

StyledProperty<string>

HotKeyProperty

Defines the HotKey property.

public static readonly StyledProperty<KeyGesture?> HotKeyProperty

Field Value

StyledProperty<KeyGesture>

IconProperty

Defines the Icon property.

public static readonly StyledProperty<object?> IconProperty

Field Value

StyledProperty<object>

InputGestureProperty

Defines the InputGesture property.

public static readonly StyledProperty<KeyGesture?> InputGestureProperty

Field Value

StyledProperty<KeyGesture>

IsCheckedProperty

Defines the IsChecked property.

public static readonly StyledProperty<bool> IsCheckedProperty

Field Value

StyledProperty<bool>

IsSubMenuOpenProperty

Defines the IsSubMenuOpen property.

public static readonly StyledProperty<bool> IsSubMenuOpenProperty

Field Value

StyledProperty<bool>

PointerEnteredItemEvent

Defines the PointerEnteredItem event.

public static readonly RoutedEvent<RoutedEventArgs> PointerEnteredItemEvent

Field Value

RoutedEvent<RoutedEventArgs>

PointerExitedItemEvent

Defines the PointerExitedItem event.

public static readonly RoutedEvent<RoutedEventArgs> PointerExitedItemEvent

Field Value

RoutedEvent<RoutedEventArgs>

StaysOpenOnClickProperty

Defines the StaysOpenOnClick property.

public static readonly StyledProperty<bool> StaysOpenOnClickProperty

Field Value

StyledProperty<bool>

SubmenuOpenedEvent

Defines the SubmenuOpened event.

public static readonly RoutedEvent<RoutedEventArgs> SubmenuOpenedEvent

Field Value

RoutedEvent<RoutedEventArgs>

ToggleTypeProperty

Defines the ToggleType property.

public static readonly StyledProperty<MenuItemToggleType> ToggleTypeProperty

Field Value

StyledProperty<MenuItemToggleType>

Properties

Command

Gets or sets the command associated with the menu item.

public ICommand? Command { get; set; }

Property Value

ICommand

CommandParameter

Gets or sets the parameter to pass to the Command property of a MenuItem.

public object? CommandParameter { get; set; }

Property Value

object

GroupName

Gets menu item group name when ToggleType is Radio.

public string? GroupName { get; set; }

Property Value

string

HasSubMenu

Gets or sets a value that indicates whether the MenuItem has a submenu.

public bool HasSubMenu { get; }

Property Value

bool

HotKey

Gets or sets an KeyGesture associated with this control

public KeyGesture? HotKey { get; set; }

Property Value

KeyGesture

Icon

Gets or sets the icon that appears in a MenuItem.

public object? Icon { get; set; }

Property Value

object

InputGesture

Gets or sets the input gesture that will be displayed in the menu item.

public KeyGesture? InputGesture { get; set; }

Property Value

KeyGesture

Remarks

Setting this property does not cause the input gesture to be handled by the menu item, it simply displays the gesture text in the menu.

IsChecked

Gets or sets if menu item is checked when ToggleType is CheckBox or Radio.

public bool IsChecked { get; set; }

Property Value

bool

IsEnabledCore

Allows a derived class to override the enabled state of the control.

protected override bool IsEnabledCore { get; }

Property Value

bool

Remarks

Derived controls may wish to disable the enabled state of the control without overwriting the user-supplied IsEnabled setting. This can be done by overriding this property to return the overridden enabled state. If the value returned from IsEnabledCore should change, then the derived control should call UpdateIsEffectivelyEnabled().

IsSelected

Gets or sets a value indicating whether the MenuItem is currently selected.

public bool IsSelected { get; set; }

Property Value

bool

IsSubMenuOpen

Gets or sets a value that indicates whether the submenu of the MenuItem is open.

public bool IsSubMenuOpen { get; set; }

Property Value

bool

IsTopLevel

Gets a value that indicates whether the MenuItem is a top-level main menu item.

public bool IsTopLevel { get; }

Property Value

bool

StaysOpenOnClick

Gets or sets a value that indicates the submenu that this MenuItem is within should not close when this item is clicked.

public bool StaysOpenOnClick { get; set; }

Property Value

bool

ToggleType

Gets toggle type of the menu item.

public MenuItemToggleType ToggleType { get; set; }

Property Value

MenuItemToggleType

Methods

Close()

Closes the submenu.

public void Close()

Remarks

This has the same effect as setting IsSubMenuOpen to false.

CreateContainerForItemOverride(object?, int, object?)

Creates or a container that can be used to display an item.

protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)

Parameters

item object
index int
recycleKey object

Returns

Control

NeedsContainerOverride(object?, int, out object?)

Determines whether the specified item can be its own container.

protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)

Parameters

item object

The item to check.

index int

The index of the item.

recycleKey object

When the method returns, contains a key that can be used to locate a previously recycled container of the correct type, or null if the item cannot be recycled. If the item is its own container then by definition it cannot be recycled, so recycleKey shoud be set to null.

Returns

bool

true if the item needs a container; otherwise false if the item can itself be used as a container.

OnApplyTemplate(TemplateAppliedEventArgs)

Called when the control's template is applied. In simple terms, this means the method is called just before the control is displayed.

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

Parameters

e TemplateAppliedEventArgs

The event args.

OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs)

Called when the styled element is added to a rooted logical tree.

protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)

Parameters

e LogicalTreeAttachmentEventArgs

The event args.

OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is added to a rooted visual tree.

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnClick(RoutedEventArgs)

Called when the MenuItem is clicked.

protected virtual void OnClick(RoutedEventArgs e)

Parameters

e RoutedEventArgs

The click event args.

OnCreateAutomationPeer()

Returns a new, type-specific AutomationPeer implementation for the control.

protected override AutomationPeer OnCreateAutomationPeer()

Returns

AutomationPeer

The type-specific AutomationPeer implementation.

OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs)

Called when the styled element is removed from a rooted logical tree.

protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)

Parameters

e LogicalTreeAttachmentEventArgs

The event args.

OnGotFocus(GotFocusEventArgs)

Called before the GotFocus event occurs.

protected override void OnGotFocus(GotFocusEventArgs e)

Parameters

e GotFocusEventArgs

The event args.

OnKeyDown(KeyEventArgs)

Handles directional navigation within the ItemsControl.

protected override void OnKeyDown(KeyEventArgs e)

Parameters

e KeyEventArgs

The key events.

OnPointerEntered(PointerEventArgs)

Called before the PointerEntered event occurs.

protected override void OnPointerEntered(PointerEventArgs e)

Parameters

e PointerEventArgs

The event args.

OnPointerExited(PointerEventArgs)

Called before the PointerExited event occurs.

protected override void OnPointerExited(PointerEventArgs e)

Parameters

e PointerEventArgs

The event args.

OnPointerReleased(PointerReleasedEventArgs)

Called before the PointerReleased event occurs.

protected override void OnPointerReleased(PointerReleasedEventArgs e)

Parameters

e PointerReleasedEventArgs

The event args.

OnPropertyChanged(AvaloniaPropertyChangedEventArgs)

Called when a avalonia property changes on the object.

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)

Parameters

change AvaloniaPropertyChangedEventArgs

The property change details.

OnSubmenuOpened(RoutedEventArgs)

Called when a submenu is opened on this MenuItem or a child MenuItem.

protected virtual void OnSubmenuOpened(RoutedEventArgs e)

Parameters

e RoutedEventArgs

The event args.

Open()

Opens the submenu.

public void Open()

Remarks

This has the same effect as setting IsSubMenuOpen to true.

UpdateDataValidation(AvaloniaProperty, BindingValueType, Exception?)

Called to update the validation state for properties for which data validation is enabled.

protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error)

Parameters

property AvaloniaProperty

The property.

state BindingValueType

The current data binding state.

error Exception

The current data binding error, if any.

Events

Click

Occurs when a MenuItem without a submenu is clicked.

public event EventHandler<RoutedEventArgs>? Click

Event Type

EventHandler<RoutedEventArgs>

PointerEnteredItem

Occurs when the pointer enters a menu item.

public event EventHandler<RoutedEventArgs>? PointerEnteredItem

Event Type

EventHandler<RoutedEventArgs>

Remarks

A bubbling version of the PointerEntered event for menu items.

PointerExitedItem

Raised when the pointer leaves a menu item.

public event EventHandler<RoutedEventArgs>? PointerExitedItem

Event Type

EventHandler<RoutedEventArgs>

Remarks

A bubbling version of the PointerExited event for menu items.

SubmenuOpened

Occurs when a MenuItem's submenu is opened.

public event EventHandler<RoutedEventArgs>? SubmenuOpened

Event Type

EventHandler<RoutedEventArgs>