-
Notifications
You must be signed in to change notification settings - Fork 3
Context Menus
A context menu is a pop-up menu that provides shortcuts for actions related to the tapped item.
- Android: Menus
- iOS: Context menus
A context menu can be attached to any Element
. In this example a context menu is attached to a Button
with one ContextMenuItem
. The ItemClickedCommand
is bound to a Command
, thus, when any item is tapped, the Command
will be executed.
<dui:Button Text="Open context menu">
<dui:ContextMenuEffect.Menu>
<dui:ContextMenu ItemClickedCommand="{Binding ItemClickedCommand}">
<dui:ContextMenuItem Title="Tap me" />
</dui:ContextMenu>
</dui:ContextMenuEffect.Menu>
</dui:Button>
There are two context menu-modes:
- Pressed (default)
- LongPressed
This mode requires people to tap to show a context menu.
<dui:Button Text="Open context menu" dui:ContextMenuEffect.Mode="Pressed">
<dui:ContextMenuEffect.Menu>
...
</dui:ContextMenuEffect.Menu>
</dui:Button>
This mode requires people to long-tap the Element
to show the context menu.
On iOS a "preview" of the
Element
will be displayed.
<dui:Button Text="Open context menu" dui:ContextMenuEffect.Mode="LongPressed">
<dui:ContextMenuEffect.Menu>
...
</dui:ContextMenuEffect.Menu>
</dui:Button>
Click events on ContextMenuItem
s can be subscribed to inside UseDIPSUI()
in MauiProgram.cs
:
...
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseDIPSUI(options =>
{
options.SetContextMenuItemClickedCallback(OnContextMenuItemClicked);
});
...
private static void OnContextMenuItemClicked(ContextMenuItem item)
{
Console.WriteLine($"Clicked context menu item with title {item.Title}!");
}
...
To attach a Context Menu to a ToolbarItem
we have created a ContextMenuToolbarItem
, which has a ContextMenu
property.
<dui:ContentPage.ToolbarItems>
<dui:ContextMenuToolbarItem Text="Tap">
<dui:ContextMenuToolbarItem.ContextMenu>
<dui:ContextMenu Title="Context menu">
<dui:ContextMenuItem Title="Item 1" />
</dui:ContextMenu>
</dui:ContextMenuToolbarItem.ContextMenu>
</dui:ContextMenuToolbarItem>
</dui:ContentPage.ToolbarItems>
NB! Attaching a Context Menu on a
ToolbarItem
is only supported when the Application is run onShell
.
- Long-Pressed Mode are not supported (yet) on ToolbarItems
Inspect the components properties class to further customize and use it.