The Silverlight Windows Phone Toolkit makes it trivially easy to add a context menu to most Windows Phone 7 controls; all you need to do, really, is add the attached dependency property to the control you want to target with the long-hold gesture, and you’re done. So if you’re looking to add a context menu for items within a list box, you just need to include it as part of the list item template, as follows:
<ListBox x:Name="MyListBox" ItemsSource="{Binding MyItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<!-- other content of your listitem goes here-->
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="MyContextMenu" Opened="MyContextMenu_Opened">
<toolkit:MenuItem Header="action" Click="contextMenuAction_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Note that you’ll need to have installed the Silverlight Windows Phone Toolkit, and added the appropriate namespace/reference at the top of your XAML file:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
And to find out which ListItem the context menu was raised from, you can use the following code (try it out in Debug mode first to make sure, though!)
ListBoxItem contextMenuListItem = MyListBox.ItemContainerGenerator.ContainerFromItem((sender as ContextMenu).DataContext) as ListBoxItem;
The controls in the Silverlight WP7 toolkit are excellent, and I highly recommend making full use of them.
Hi Henry,
I have smartphone apps on Google, RIM, & Amazon.
Here is a BlackBerry Menu:
public void makeMenu(Menu menu, int instance) {
numOfStations = station.LoadStations(rangeName);
MenuItem[] menuItems = new MenuItem[numOfStations];
int ord = 31900;
if(instance == 0) {
for(int i = 0; i < numOfStations; i++) {
load = station.getStation(i).station;
menuItems[i] = new MenuItem(new StringProvider(load), ord++, 108) {
String runStation = load;
public void run() {
SelectedStation(runStation);
}
};
menu.add(menuItems[i]);
}
super.makeMenu(menu, instance);
}
}
Here is an Android Menu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
numOfStations = station.LoadStations(range);
for(int i = 0; i < numOfStations; i++)
menu.add(0, i, i, station.getStation(i).station).setIcon(android.R.drawable.button_onoff_indicator_on);
return super.onCreateOptionsMenu(menu);
}
Henry, why can’t I find a Windows 7 Phone Menu for C#???
Thanks,
Austin MCP (of all things)
austin@mytideschedule.com
Hi Austin, I believe you’re looking for the ApplicationBar – have a look at the following MSDN article for a quick starter (http://msdn.microsoft.com/en-us/library/ff431786(v=vs.92).aspx)
Best of luck!
This is very handy – exactly what I needed! However, in your one-liner to find the sender, you have an extra parenthesis on the end of the clause. Also, I couldn’t get it to work coercing the sender to a ContextMenu – I had to coerce it to a MenuItem. So mine looks like (C#):
ListBoxItem contextMenuListItem = (ListBoxItem)(lbRecentClimbs.ItemContainerGenerator.ContainerFromItem(((MenuItem)sender).DataContext));
Hi Chris,
Thanks for spotting that! Copy/paste/retyping fail :P
[...] Informationen darüber, wie man ein ContextMenu-Control einbaut in seine Applikation, findet man z.B. hier. Bei einer ListBox ist es noch ein wenig tricky herauszufinden, für welches Item das Menu aufgerufen wurde, aber der nötige Trick findet sich z.B. hier. [...]