HomeWindowsContext Menu in Windows Phone

Context Menu in Windows Phone

The Context menu is part of the Silverlight Toolkit for Windows Phone controls which can be downloaded from the codeplex site

silverlight.codeplex.com

When the user holds down on a item / panel / control fro few seconds , a pop up menu is opened which shows some options ..

One of the simple scenario where context menu is used in Wndows Phone is when you want to pin the App to the Start screen or unpin it from the start screen .

Context Menu in Windows Phone

To create , the context menu dynamically , follow the below steps

1. Add the reference to the namespace Microsoft.Phone.Controls;

2. Create an instance of Context Menu

ContextMenu PopUpMenu = new ContextMenu();

3. Create Menu Items and also the event handler for the Click event of menu item

MenuItem item1 = new MenuItem() { Header = "Android" };
item1.Click += new RoutedEventHandler(menuItem1_Click);
MenuItem item2 = new MenuItem() { Header = "Windows Phone" };
item2.Click += new RoutedEventHandler(menuItem1_Click);
MenuItem item3 = new MenuItem() { Header = "iPhone" };
item3.Click += new RoutedEventHandler(menuItem1_Click);

4. Add the menu items to the Context Menu

PopUpMenu.Items.Add(item1);
PopUpMenu.Items.Add(item2);
PopUpMenu.Items.Add(item3);

5. Set the Context Menu for the Items , In this example , i use the text box

ContextMenuService.SetContextMenu(this.textBox1, PopUpMenu);

So when the user holds on the text box for few seconds , the context menu will show the menu items and when the user click on one of the memu item , it is copied to the textbox .

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
ContextMenu PopUpMenu = new ContextMenu();
MenuItem item1 = new MenuItem() { Header = "Android" };
item1.Click += new RoutedEventHandler(menuItem1_Click);
MenuItem item2 = new MenuItem() { Header = "Windows Phone" };
item2.Click += new RoutedEventHandler(menuItem1_Click);
MenuItem item3 = new MenuItem() { Header = "iPhone" };
item3.Click += new RoutedEventHandler(menuItem1_Click);
PopUpMenu.Items.Add(item1);
PopUpMenu.Items.Add(item2);
PopUpMenu.Items.Add(item3);
ContextMenuService.SetContextMenu(this.textBox1, PopUpMenu);
}
void menuItem1_Click(object sender, RoutedEventArgs e)
{
MenuItem itm = (sender as MenuItem);
textBox1.Text = itm.Header.ToString();
}

<iframe width=”420″ height=”315″ src=”http://www.youtube.com/embed/q3YtFVe5bnE” frameborder=”0″></iframe>

    1 Comment

  1. Frank Vinnicyus
    August 23, 2013
    Reply

    Thanks a lot, man!

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...