Mapping ContextMenu

I find it difficult to find good documentation for this, despite some time.

I would like to have a context menu in my application that replicates the behavior observed using other tap-and-hold context menus, for example, attaching an application to the start screen from the list of applications.

Here is my context menu:

<toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu x:Name="sectionContextMenu"> <toolkit:MenuItem Header="Hide this section from this list" /> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> 

How do I show it?

+4
source share
2 answers

The context menu should be attached to the element that you want the user to press and hold.

 <Border Margin="0,12" BorderBrush="{StaticResource PhoneForegroundBrush}" BorderThickness="2" Background="Transparent" VerticalAlignment="Center" Padding="16"> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu x:Name="sectionContextMenu"> <toolkit:MenuItem Header="Hide this section from this list" /> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <TextBlock Text="Tap and hold here to invoke a ContextMenu" Style="{StaticResource PhoneTextNormalStyle}"/> </Border> 

Now the user can call the context menu by pressing and holding the contents of this Border element.

+7
source

Unique context menu for different items depending on content.

 private ContextMenu CreateContextMenu(ListBoxItem lbi) { ContextMenu contextMenu = new ContextMenu(); ContextMenuService.SetContextMenu(lbi, contextMenu); contextMenu.Padding = new Thickness(0); string item_1 = "item 1"; if(lbi.Content is string) { item_1 = lbi.Content as string; } contextMenu.ItemsSource = new List<string> { item_1, "item 2", "item 3" }; contextMenu.IsOpen = true; return contextMenu; } private void Results_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (Results.SelectedIndex == -1) return; int index = Results.SelectedIndex; ListBoxItem lbi = Results.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; CreateContextMenu(lbi); Results.SelectedIndex = -1; } 
+2
source

Source: https://habr.com/ru/post/1332674/


All Articles