# Target
In WP7, ContextMenu can be used to achieve the effect of the long-pressed pop-up option menu.
# Prerequisites
For toolkit with ContextMenu, visit http://silverlight.codeplex.com/releases/view/71550to get the latest toolkit to get the correct source code and xap.
If you need to check, you can access $: \ Program Files \ Reference Assemblies \ Microsoft \ Framework \ Silverlight \ v4.0 \ Profile (where $ is the SDK installation drive letter ). For WP7.0, access the WindowsPhone directory; For WP7.1, access the WindowsPhone71 directory.
# Implementation process
Now, the preparation is ready and you can start to implement the effect.
Method 1: use the code to implement ContextMenuService
- ContextMenu menu = new ContextMenu ();
- MenuItem menuItem1 = new MenuItem ();
- MenuItem1.Header = "This is Menu Item 1 ";
- MenuItem1.Click + = new RoutedEventHandler (MenuItem_Click );
- Menu. Items. Add (menuItem1 );
- MenuItem menuItem2 = new MenuItem ();
- MenuItem2.Header = "This is Menu Item 2 ";
- MenuItem2.Click + = new RoutedEventHandler (MenuItem_Click );
- Menu. Items. Add (menuItem2 );
- ContextMenuService. SetContextMenu (rc, menu); // The rc here indicates the name of the rectangle box, or the name of the Control attached to the ContextMenu.
Method 2: Modify the xaml file to implement
1. Introduce Microsoft. Phone. Controls. Toolkit in project references.
2. Add the toolkit declaration to the page (for example, demo. xaml) file to be implemented as follows:
- Xmlns: toolkit = "clr-namespace: Microsoft. Phone. Controls; assembly = Microsoft. Phone. Controls. Toolkit"
3. Add the implementation code in demo. xaml as follows:
- <! -- ContentPanel-place additional content here -->
- <StackPanelx: Name = "ContentPanel" Grid. Row = "1" Margin = "12, 0, 12, 0">
- <GridBackground = "Blue">
- <TextBlockText = "Select a color from the Context Menu"/>
- <ListBoxx: Name = "listBox">
- <ListBox. ItemTemplate>
- <DataTemplate>
- <StackPanelOrientation = "Horizontal">
- <Toolkit: ContextMenuService. ContextMenu>
- <Toolkit: ContextMenu>
- <Toolkit: MenuItemHeader = "Add Color" Click = "MenuItem_Click"/>
- <Toolkit: MenuItemHeader = "Remove Color" Click = "MenuItem_Click"/>
- </Toolkit: ContextMenu>
- </Toolkit: ContextMenuService. ContextMenu>
- <ImageSource = "{Binding ImageUri}" Stretch = "None"/>
- <TextBlockText = "{Binding Text}"/>
- </StackPanel>
- </DataTemplate>
- </ListBox. ItemTemplate>
- </ListBox>
- </Grid>
- </StackPanel>
In this way, the effect of the long-press pop-up menu can be completed.
# Effect demonstration
Image Sharing:
# Problem
1. In WP7.0, the scaled part is displayed, which is more obvious when the background color is large.
2. The effect on mango (WP7.1 +) is very good, consistent with the system phenomenon. It seems that Microsoft has fixed this issue.
# Reference
1. http://silverlight.codeplex.com/releases/view/71550, to get the latest toolkit and source code.
2. http://www.windowsphonegeek.com/tips/wp7-contextmenu-answers-to-popular-questions