Implement the pop-up ContextMenu effect in long-pressed Listbox on WP7

Source: Internet
Author: User

# 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

  1. ContextMenu menu = new ContextMenu ();
  2. MenuItem menuItem1 = new MenuItem ();
  3. MenuItem1.Header = "This is Menu Item 1 ";
  4. MenuItem1.Click + = new RoutedEventHandler (MenuItem_Click );
  5. Menu. Items. Add (menuItem1 );
  6. MenuItem menuItem2 = new MenuItem ();
  7. MenuItem2.Header = "This is Menu Item 2 ";
  8. MenuItem2.Click + = new RoutedEventHandler (MenuItem_Click );
  9. Menu. Items. Add (menuItem2 );
  10. 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:

  1. Xmlns: toolkit = "clr-namespace: Microsoft. Phone. Controls; assembly = Microsoft. Phone. Controls. Toolkit"

3. Add the implementation code in demo. xaml as follows:

  1. <! -- ContentPanel-place additional content here -->
  2. <StackPanelx: Name = "ContentPanel" Grid. Row = "1" Margin = "12, 0, 12, 0">
  3. <GridBackground = "Blue">
  4. <TextBlockText = "Select a color from the Context Menu"/>
  5. <ListBoxx: Name = "listBox">
  6. <ListBox. ItemTemplate>
  7. <DataTemplate>
  8. <StackPanelOrientation = "Horizontal">
  9. <Toolkit: ContextMenuService. ContextMenu>
  10. <Toolkit: ContextMenu>
  11. <Toolkit: MenuItemHeader = "Add Color" Click = "MenuItem_Click"/>
  12. <Toolkit: MenuItemHeader = "Remove Color" Click = "MenuItem_Click"/>
  13. </Toolkit: ContextMenu>
  14. </Toolkit: ContextMenuService. ContextMenu>
  15. <ImageSource = "{Binding ImageUri}" Stretch = "None"/>
  16. <TextBlockText = "{Binding Text}"/>
  17. </StackPanel>
  18. </DataTemplate>
  19. </ListBox. ItemTemplate>
  20. </ListBox>
  21. </Grid>
  22. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.