WPF WebView2 - not able to disable Context Menu

Viewed 4316

Environment: Windows 10 Pro - ver. 1903, VS2019 latest version, WPF Core

When I have a manual control on an HTML page displayed inside WebView2 control, I can disable the context menu by simply adding oncontextmunu={return false;} as an attribute to <body> tag. But in most cases the web page is displayed based on user interaction (what page user navigates to etc.).

Question: How can we disable to context menu of a webpage displayed in WebView2 control?

Remarks:

  1. I have noticed that in some cases where a JavaScript is loaded along with a webpage, the oncontextmunu={return false;} does not disable the context menu even if I add this attribute programmatically to the body tag of loaded page. I see AreDefaultContextMenusEnabled property of Microsoft.Web.WebView2.Core.CoreWebView2Settings class but VS2019 does not recognize any such property for WebView2.
  2. There was a similar issue on the old WebBrowser control of WPF as described here and here, and I have the similar situation for WebView2.
  3. According to bullet 5 of this Microsoft link the issue was resolved. But the solution there seems to be related to C++ while I'm using C#. Maybe, there is an alternative.
1 Answers

Mostly, the reason for disabling the Context Menu is for preventing the End-User from inspecting the page source. Thus, we should also disable the accelerator keys using both instructions:

webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;

since the End-User can use Ctrl+Shift-C and F12 for DevTools check Accelerator Keys

Related