Skip to content

Commit e019355

Browse files
author
Hosam Tageldin
committed
Added more complete sample code to wpf custom menu sample
1 parent ec6380a commit e019355

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

specs/ContextMenuRequested.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,25 +247,53 @@ The developer can use the data provided in the Event arguments to display a cust
247247
```c#
248248
webView.CoreWebView2.ContextMenuRequested += delegate (object sender, CoreWebView2ContextMenuRequestedEventArgs args)
249249
{
250-
CoreWebView2Deferral deferral = args.GetDeferral();
251250
IList<CoreWebView2ContextMenuItem> menuList = args.MenuItems;
251+
CoreWebView2Deferral deferral = args.GetDeferral();
252252
args.Handled = true;
253-
ContextMenu cm = this.FindResource("ContextMenu") as ContextMenu;
254-
cm.Items.Clear();
253+
ContextMenu cm = new ContextMenu();
254+
cm.Closed += (s, ex) => deferral.Complete();
255+
PopulateContextMenu(args, menuList, cm);
256+
cm.IsOpen = true;
257+
};
258+
void PopulateContextMenu(CoreWebView2ContextMenuRequestedEventArgs args,
259+
IList<CoreWebView2ContextMenuItem> menuList, ItemsControl cm)
260+
{
255261
for (int i = 0; i < menuList.Count; i++)
256262
{
257263
CoreWebView2ContextMenuItem current = menuList[i];
264+
if (current.Kind == CoreWebView2ContextMenuItemKind.Separator)
265+
{
266+
Separator sep = new Separator();
267+
cm.Items.Add(sep);
268+
continue;
269+
}
258270
MenuItem newItem = new MenuItem();
259-
newItem.Header = current.Label;
260-
newItem.Click += (s, ex) =>
271+
// The accessibility key is the key after the & in the label
272+
// Replace with '_' so it is underlined in the label
273+
newItem.Header = current.Label.Replace('&', '_');
274+
newItem.InputGestureText = current.ShortcutKeyDescription;
275+
newItem.IsEnabled = current.IsEnabled;
276+
if (current.Kind == CoreWebView2ContextMenuItemKind.Submenu)
277+
{
278+
PopulateContextMenu(args, current.Children, newItem);
279+
}
280+
else
261281
{
262-
args.SelectedCommandId = current.CommandId;
263-
deferral.Complete();
264-
};
282+
if (current.Kind == CoreWebView2ContextMenuItemKind.Checkbox
283+
|| current.Kind == CoreWebView2ContextMenuItemKind.Radio)
284+
{
285+
newItem.IsCheckable = true;
286+
newItem.IsChecked = current.IsChecked;
287+
}
288+
289+
newItem.Click += (s, ex) =>
290+
{
291+
args.SelectedCommandId = current.CommandId;
292+
};
293+
}
265294
cm.Items.Add(newItem);
266295
}
267-
cm.IsOpen = true;
268-
};
296+
}
269297
```
270298
# Remarks
271299

@@ -311,7 +339,7 @@ The developer can use the data provided in the Event arguments to display a cust
311339
/// Context menu item descriptor for "Print" action.
312340
COREWEBVIEW2_CONTEXT_MENU_ITEM_DESCRIPTOR_PRINT,
313341

314-
/// Context menu item descriptor for "Creating a QR code" action.
342+
/// Context menu item descriptor for "Create a QR code" action.
315343
COREWEBVIEW2_CONTEXT_MENU_ITEM_DESCRIPTOR_CREATE_QR_CODE,
316344

317345
/// Context menu item descriptor for "Inspect" action.

0 commit comments

Comments
 (0)