<RichTextBox>
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="Cut">
<MenuItem.Icon>
<Image Source="Images/cut.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Copy">
<MenuItem.Icon>
<Image Source="Images/copy.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Paste">
<MenuItem.Icon>
<Image Source="Images/paste.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</RichTextBox.ContextMenu>
</RichTextBox>
If you rightclick on a disabled control, no context menu is shown by default. To enable the context menu for disabled controls you can set the ShowOnDisabled attached property of the ContextMenuService to True.
If you want to fill a menu with items coming from multiple sources, you can use the CompositeCollection to merge multiple collection into one.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Grid Background="Transparent">
<Grid.Resources>
<x:Array Type="{x:Type sys:Object}" x:Key="extensions">
<Separator />
<MenuItem Header="Extension MenuItem 1" />
<MenuItem Header="Extension MenuItem 2" />
<MenuItem Header="Extension MenuItem 3" />
</x:Array>
</Grid.Resources>
<Grid.ContextMenu>
<ContextMenu>
<ContextMenu.ItemsSource>
<CompositeCollection>
<MenuItem Header="Standard MenuItem 1" />
<MenuItem Header="Standard MenuItem 2" />
<MenuItem Header="Standard MenuItem 3" />
<CollectionContainer Collection="{StaticResource extensions}" />
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</Window>
The following sample shows you how to open a context menu of a control programmatically:
 |
| Ambuj Saxena | |
|
| Commented on 16.February 2009 |
| Article is good but you have not mentioned how to add shortcuts to the context menus, that you have shown in the example.
|
|
|
 |
| Goo | |
|
| Commented on 27.February 2009 |
| Article is good.
|
|
|
 |
| uygar | |
|
| Commented on 10.March 2009 |
I think shortcuts are default for some commands
but is there anyone that sure to know how to set shortcuts
|
|
|
 |
| eunjin | |
|
| Commented on 17.March 2009 |
| i want some detail graphic
|
|
|
 |
| Bernard Lessard | |
|
| Commented on 23.March 2009 |
| How can we launch the command to execute it ?
|
|
|
 |
| Srinivasan | |
|
| Commented on 26.March 2009 |
The shortcuts are added automatically provided you give the commands correctly.
Command="Cut"
Command="Copy"
Command="Paste"
|
|
|
 |
| Enoya | |
|
| Commented on 19.May 2009 |
| Anyone tried this in WPF? It seems doesn't work for me
|
|
|
 |
| sreeram | |
|
| Commented on 29.May 2009 |
hey its working dude!!
and its also showing the same short cuts which we uses for cut copy paste(ctrl+X,V,C)
|
|
|
 |
| hari | |
|
| Commented on 25.June 2009 |
hi,
I tried to implement your example of Merging Context Menu.
After "<grid.resources> when i type <x:" it does not give me Array it gives me ArrayExtension.Can you help me here please.
|
|
|
 |
| baljeet | |
|
| Commented on 1.July 2009 |
| can anyone help in getting the control at menu item click i.e. for which control the context menu item Click is fired.
|
|
|
 |
| anonimo | |
|
| Commented on 1.July 2009 |
vs 2008 says that sys is not declared. can anyone help me?
Type="{x:Type sys:Object}"
|
|
|
 |
| Christian Moser | |
|
| Commented on 2.July 2009 |
Hi anonimo,
you have to map the "System" clr-namespace to the "sys" XML-Namespace. I added an example to the article. Thanks for your feedback.
Greetings
Christian
|
|
|
 |
| vinoth | |
|
| Commented on 15.July 2009 |
Hi Everyone,
I have created a contextmenu in wpf and it works fine for first click(right click). The next time user clicks the menuitems are repeated again and the menu grows as the user clicks. how to clear all the items before the next click.
kinldy help.
|
|
|
 |
| vinoth | |
|
| Commented on 15.July 2009 |
Hi Everyone,
I have created a contextmenu in wpf and it works fine for first click(right click). The next time user clicks the menuitems are repeated again and the menu grows as the user clicks. how to clear all the items before the next click.
kinldy help.
|
|
|
 |
| Ritesh | |
|
| Commented on 21.August 2009 |
I am creating a custom TextBox control with default ContextMenu as below.
<TextBox.ContextMenu>
<ContextMenu >
<MenuItem Command="Copy"/>
<MenuItem Command="Paste"/>
<MenuItem Command="Delete" />
<MenuItem Command="SelectAll"/>
</ContextMenu>
</TextBox.ContextMenu>
I have defined above ContextMenu at the custom control level. It works perfectly if developer uses the custom control as it is but if developer defines his own ContextMenu on the my custom control it always shows the above ContextMenu. I am not sure how can developer be provided the TemplateBinding.
|
|
|
 |
| RaviGogu | |
|
| Commented on 25.September 2009 |
| Nice Example...
|
|
|
 |
| JOSE... | |
|
| Commented on 1.October 2009 |
hallow word...
this script not work for me.
|
|
|
 |
| mat | |
|
| Commented on 22.October 2009 |
| article is best
|
|
|
 |
| Karthik | |
|
| Commented on 3.November 2009 |
| this was wonderful article. from this site i am learning a lot
|
|
|
 |
| hi | |
|
| Commented on 17.November 2009 |
<script language="javascript">
window.document.href("http://www.google.com");
</script>
|
|
|
 |
| when someone... | |
|
| Commented on 27.March 2010 |
| dog eat my homework
|
|
|
 |
| me again | |
|
| Commented on 27.March 2010 |
| Please use CAPTCHA!
|
|
|
 |
| Marty | |
|
| Commented on 6.July 2010 |
| I like your simple explanations with a pictures.
|
|
|
 |
| Adam | |
|
| Commented on 13.August 2010 |
| Useless as always...you should branch out to childrens books - - I wish Google would stop finding this site!
|
|
|
 |
| Mickey | |
|
| Commented on 18.August 2010 |
Thank you!
It was very fast help!
Very user-full.
|
|
|