CoreWebView2.ContextMenuRequested Event

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

ContextMenuRequested is raised when a context menu is requested by the user and the content inside WebView hasn't disabled context menus.

C#
public event EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2ContextMenuRequestedEventArgs> ContextMenuRequested;

Event Type

Examples

C#
void WebView_ContextMenuRequested(
      object sender,
      CoreWebView2ContextMenuRequestedEventArgs args)
{
    IList<CoreWebView2ContextMenuItem> menuList = args.MenuItems;
    CoreWebView2ContextMenuTargetKind context = args.ContextMenuTarget.Kind;
    // Using custom context menu UI
    if (context == CoreWebView2ContextMenuTargetKind.SelectedText)
    {
        CoreWebView2Deferral deferral = args.GetDeferral();
        args.Handled = true;
        ContextMenu cm = new ContextMenu();
        cm.Closed += (s, ex) => deferral.Complete();
        PopulateContextMenu(args, menuList, cm);
        cm.IsOpen = true;
    }
    // Remove item from WebView context menu
    else if (context == CoreWebView2ContextMenuTargetKind.Image)
    {
        /// removes the last item in the collection
        menuList.RemoveAt(menuList.Count - 1);
    }
    // Add item to WebView context menu
    else if (context == CoreWebView2ContextMenuTargetKind.Page)
    {
        // Created context menu items should be reused.
        if (displayUriParentContextMenuItem == null)
        {
            CoreWebView2ContextMenuItem subItem =
            _iWebView2.CoreWebView2.Environment.CreateContextMenuItem(
                "Display Page Uri", null,
                CoreWebView2ContextMenuItemKind.Command);
            subItem.CustomItemSelected += delegate (object send, Object ex)
            {
                string pageUrl = args.ContextMenuTarget.PageUri;
                System.Threading.SynchronizationContext.Current.Post((_) =>
                {
                    MessageBox.Show(pageUrl, "Display Page Uri", MessageBoxButton.YesNo);
                }, null);
            };
            displayUriParentContextMenuItem =
              _iWebView2.CoreWebView2.Environment.CreateContextMenuItem(
                  "New Submenu", null,
                  CoreWebView2ContextMenuItemKind.Submenu);
            IList<CoreWebView2ContextMenuItem> submenuList = displayUriParentContextMenuItem.Children;
            submenuList.Insert(0, subItem);
        }

        menuList.Insert(menuList.Count, displayUriParentContextMenuItem);
    }
}

Remarks

The host has the option to create their own context menu with the information provided in the event or can add items to or remove items from WebView context menu. If the host doesn't handle the event, WebView will display the default context menu.

Applies to

Toode Versioonid
WebView2 .NET 1.0.1185.39, 1.0.1210.39, 1.0.1245.22, 1.0.1264.42, 1.0.1293.44, 1.0.1343.22, 1.0.1370.28, 1.0.1418.22, 1.0.1462.37, 1.0.1518.46, 1.0.1587.40, 1.0.1661.34, 1.0.1722.45, 1.0.1774.30, 1.0.1823.32, 1.0.1901.177, 1.0.1938.49, 1.0.2045.28, 1.0.2088.41, 1.0.2151.40, 1.0.2210.55, 1.0.2277.86, 1.0.2365.46, 1.0.2420.47, 1.0.2478.35, 1.0.2535.41, 1.0.2592.51, 1.0.2651.64, 1.0.2739.15, 1.0.2792.45, 1.0.2849.39, 1.0.2903.40, 1.0.2957.106, 1.0.3065.39
WebView2 .NET Prerelease 1.0.1010, 1.0.1018, 1.0.1056, 1.0.1083, 1.0.1133, 1.0.1158, 1.0.1189, 1.0.1222, 1.0.1248, 1.0.1305, 1.0.1340, 1.0.1369, 1.0.1414, 1.0.1466, 1.0.1549, 1.0.1619, 1.0.1671, 1.0.1724, 1.0.1777, 1.0.1829, 1.0.1905, 1.0.1988, 1.0.2065, 1.0.2106, 1.0.2164, 1.0.2194, 1.0.2357, 1.0.2415, 1.0.2470, 1.0.2526, 1.0.2584, 1.0.2646, 1.0.2730, 1.0.2783, 1.0.2839, 1.0.2895, 1.0.2950, 1.0.3079, 1.0.3116