Skip to main content

ContextMenu

The ContextMenu can be applied to any host control to implement a right-click 'context sensitive' menu. This uses an attached property of the host control.

info

To review the concept behind this use of an attached property, see here.

Example

In this example, a context menu is attached to a multi-line text box. Right-click your mouse in the preview area to see the context menu.

<UserControl xmlns="https://github.com/avaloniaui">
  <TextBox AcceptsReturn="True" TextWrapping="Wrap">
    <TextBox.ContextMenu>
      <ContextMenu>
        <MenuItem Header="Copy"/>
        <MenuItem Header="Paste"/>
      </ContextMenu>
    </TextBox.ContextMenu>     
  </TextBox>
</UserControl>
Preview
Loading Avalonia Preview...

Context Flyout

You can use a context flyout as an alternative to a context menu. A context flyout can provide a sharable and richer UI experience than a simple context menu.

warning

A control cannot have a context flyout and a context menu attached at the same time.

A context flyout is invoked automatically like a context menu.

More Information

info

For the complete API documentation about this control, see here.

info

View the source code on GitHub ContextMenu.cs

Discussion

Have questions or feedback? Join the conversation below.