Class MenuItem
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
CommandParameterProperty
Defines the CommandParameter property.
public static readonly StyledProperty<object?> CommandParameterProperty
Field Value
CommandProperty
Defines the Command property.
public static readonly StyledProperty<ICommand?> CommandProperty
Field Value
GroupNameProperty
Defines the GroupName property.
public static readonly StyledProperty<string?> GroupNameProperty
Field Value
HotKeyProperty
Defines the HotKey property.
public static readonly StyledProperty<KeyGesture?> HotKeyProperty
Field Value
IconProperty
Defines the Icon property.
public static readonly StyledProperty<object?> IconProperty
Field Value
InputGestureProperty
Defines the InputGesture property.
public static readonly StyledProperty<KeyGesture?> InputGestureProperty
Field Value
IsCheckedProperty
Defines the IsChecked property.
public static readonly StyledProperty<bool> IsCheckedProperty
Field Value
IsSubMenuOpenProperty
Defines the IsSubMenuOpen property.
public static readonly StyledProperty<bool> IsSubMenuOpenProperty
Field Value
PointerEnteredItemEvent
Defines the PointerEnteredItem event.
public static readonly RoutedEvent<RoutedEventArgs> PointerEnteredItemEvent
Field Value
PointerExitedItemEvent
Defines the PointerExitedItem event.
public static readonly RoutedEvent<RoutedEventArgs> PointerExitedItemEvent
Field Value
StaysOpenOnClickProperty
Defines the StaysOpenOnClick property.
public static readonly StyledProperty<bool> StaysOpenOnClickProperty
Field Value
SubmenuOpenedEvent
Defines the SubmenuOpened event.
public static readonly RoutedEvent<RoutedEventArgs> SubmenuOpenedEvent
Field Value
ToggleTypeProperty
Defines the ToggleType property.
public static readonly StyledProperty<MenuItemToggleType> ToggleTypeProperty
Field Value
Properties
Command
Gets or sets the command associated with the menu item.
public ICommand? Command { get; set; }
Property Value
CommandParameter
public object? CommandParameter { get; set; }
Property Value
GroupName
Gets menu item group name when ToggleType is Radio.
public string? GroupName { get; set; }
Property Value
HasSubMenu
Gets or sets a value that indicates whether the MenuItem has a submenu.
public bool HasSubMenu { get; }
Property Value
HotKey
Gets or sets an KeyGesture associated with this control
public KeyGesture? HotKey { get; set; }
Property Value
Icon
Gets or sets the icon that appears in a MenuItem.
public object? Icon { get; set; }
Property Value
InputGesture
Gets or sets the input gesture that will be displayed in the menu item.
public KeyGesture? InputGesture { get; set; }
Property Value
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
IsEnabledCore
Allows a derived class to override the enabled state of the control.
protected override bool IsEnabledCore { get; }
Property Value
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
IsSubMenuOpen
Gets or sets a value that indicates whether the submenu of the MenuItem is open.
public bool IsSubMenuOpen { get; set; }
Property Value
IsTopLevel
Gets a value that indicates whether the MenuItem is a top-level main menu item.
public bool IsTopLevel { get; }
Property Value
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
ToggleType
Gets toggle type of the menu item.
public MenuItemToggleType ToggleType { get; set; }
Property Value
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
Returns
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
objectThe item to check.
index
intThe index of the item.
recycleKey
objectWhen 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
TemplateAppliedEventArgsThe event args.
OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs)
Called when the styled element is added to a rooted logical tree.
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
Parameters
e
LogicalTreeAttachmentEventArgsThe event args.
OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)
Called when the control is added to a rooted visual tree.
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
Parameters
e
VisualTreeAttachmentEventArgsThe event args.
OnClick(RoutedEventArgs)
Called when the MenuItem is clicked.
protected virtual void OnClick(RoutedEventArgs e)
Parameters
e
RoutedEventArgsThe 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
LogicalTreeAttachmentEventArgsThe event args.
OnGotFocus(GotFocusEventArgs)
Called before the GotFocus event occurs.
protected override void OnGotFocus(GotFocusEventArgs e)
Parameters
e
GotFocusEventArgsThe event args.
OnKeyDown(KeyEventArgs)
Handles directional navigation within the ItemsControl.
protected override void OnKeyDown(KeyEventArgs e)
Parameters
e
KeyEventArgsThe key events.
OnPointerEntered(PointerEventArgs)
Called before the PointerEntered event occurs.
protected override void OnPointerEntered(PointerEventArgs e)
Parameters
e
PointerEventArgsThe event args.
OnPointerExited(PointerEventArgs)
Called before the PointerExited event occurs.
protected override void OnPointerExited(PointerEventArgs e)
Parameters
e
PointerEventArgsThe event args.
OnPointerReleased(PointerReleasedEventArgs)
Called before the PointerReleased event occurs.
protected override void OnPointerReleased(PointerReleasedEventArgs e)
Parameters
e
PointerReleasedEventArgsThe event args.
OnPropertyChanged(AvaloniaPropertyChangedEventArgs)
Called when a avalonia property changes on the object.
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
Parameters
change
AvaloniaPropertyChangedEventArgsThe 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
RoutedEventArgsThe 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
AvaloniaPropertyThe property.
state
BindingValueTypeThe current data binding state.
error
ExceptionThe current data binding error, if any.
Events
Click
Occurs when a MenuItem without a submenu is clicked.
public event EventHandler<RoutedEventArgs>? Click
Event Type
PointerEnteredItem
Occurs when the pointer enters a menu item.
public event EventHandler<RoutedEventArgs>? PointerEnteredItem
Event Type
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
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