WP7 ContextMenu with complex MenuItems
published on: 6/14/2011 | Views: N/A | Tags: WP7Toolkit
by WindowsPhoneGeek
In this quick tip I am going to discuss some questions regarding the WP7 ContextMenu and its MenuItems.
NOTE: Before we begin let me first mention that ContextMenu is a part of the Silverlight for Windows Phone 7 toolkit. You can also take a look at our WP7 ContextMenu in depth | Part1: key concepts and API and WP7 ContextMenu: answers to popular questions articles for reference.
We received lots of questions related to ContextMenu and especially its MenuItems. Here are some of them:
Question 1: In a data bound ListBox how to add a ContextMenu with Images?
Question 2: How can I add more complex items like Images, Buttons, etc inside a Context Menu via MenuItem?
Question 3: ContextMenu throws an exception when I try to add Images inside a MenuItem?
What is the problem?
It is generally believed that all you need to do in order to have complex items inside a ContextMenu is just to add them between the opening and closing tags of the MenuItem. However this is not true, if you try to do this you will notice the following exception(although the project is build successfully):
This is happening because the only way in which you can set any content to a MenuItem is either using its Header property or HeaderTemplate because MenuItem is in itself a HeaderedItemsControl not a ContentControl!
The Solution
So just use the Header or HeaderTemplate property of the MenuItem like for example:
Here is how the full code should look like when used inside a data bound ListBox:
<ListBox x:Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="AddItem"/>
<toolkit:MenuItem>
<toolkit:MenuItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Delete Item"/>
<Image Source="Images/appbar.delete.rest.png"/>
</StackPanel>
</toolkit:MenuItem.Header>
</toolkit:MenuItem>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
NOTE: It is important to set the right Image Path depending on the build action of the target Image: WP7 working with Images: Content vs Resource build action
You can download the full source code here:
I hope that the tip was helpful.
You can also follow us on Twitter @winphonegeek
Comments
MenuItem Header
posted by: Memed Alii on 6/14/2011 6:28:08 PM
Thank you for answering these questions.
I did not know how to add an Image to the ManuItem. The problem was that I was able to build my project but when running it the exception was thrown. My mistake was that I did not know about the Header property and expected everything to work without it. Once again thank you for explanation and providing sample code as well.
Need Help with ContextMenu
posted by: Pam on 6/14/2011 6:29:54 PM
I am new to Windows Phone 7 development. I was not able to find out any documentation or sample code of how to use ContextMenu. Can you please guide me?
RE:Need Help with ContextMenu
posted by: WindowsPhoneGeek on 6/14/2011 6:39:18 PM
You can take a look at our articles:
Thanks for providing the links
posted by: Pam on 6/14/2011 9:21:29 PM
Great! Thank you for providing the links. Relay helpful stuff, I am starting reading now..
Context Menu Help
posted by: lane on 9/6/2011 5:36:46 PM
Hi, I created a listbox via code, assigned itemteplate to it dynamicaly(by parsing xaml)and databinded it with a collection using ItemsSource propery. i also assigned context menu to the list box.
I am unable to get the binded collection item from context menu click event. the ItemContainerStyle of the list box couldnt be evaluated.
Please Help
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
