Click here to Skip to main content
13,354,605 members (58,012 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 28 Nov 2009

Silverlight 4: How to use the all new right click context menu

, 28 Nov 2009
Rate this:
Please Sign up or sign in to vote.
This article demonstrates the all new right click feature of Silverlight 4.


In my previous posts, I discussed about “How to work with the Notification API” and “How to Capture Video from the Default Webcam”. In this post, I will describe another cool new feature: How to use the all new right click context menu of Silverlight 4.


Silverlight 4 has now support for right click. You can now register the events “MouseRightButtonDown” and “MouseRightButtonUp” to any FrameworkElement. Hence, no need to use JavaScript to disable the right click option. If you want to disable the right click option, then just implement those events with:

e.Handled = true;

Code of Use

Now, if you want to implement a context menu on right click, create the popup context menu and position it in the right location. The following code will create the context menu:

private Popup CreateContextMenu(Point currentMousePosition)
    Popup popup = new Popup();
    Grid popupGrid = new Grid();
    Canvas popupCanvas = new Canvas();

    popup.Child = popupGrid;
    popupCanvas.MouseLeftButtonDown += (sender, e) => { HidePopup(); };
    popupCanvas.MouseRightButtonDown += (sender, e) => { e.Handled = true; HidePopup(); };
    popupCanvas.Background = new SolidColorBrush(Colors.Transparent);

    popupGrid.Width = Application.Current.Host.Content.ActualWidth;
    popupGrid.Height = Application.Current.Host.Content.ActualHeight;
    popupCanvas.Width = popupGrid.Width;
    popupCanvas.Height = popupGrid.Height;

    return popup;

CreateContextMenuItems() will add some context menu items. Clicking on it will show which menu item was clicked by you. Up to this point, I had only talked about the creation of the customized context menu. Now we have to show it on right click inside the Silverlight application. In my example, I added a Border control which has the right click event registered. Now, check the code below which is responsible for showing the context menu:

void brdRightClickZone_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    txbMessage.Text = "Right Clicked";
    Point currentMousePosition = e.GetPosition(LayoutRoot);

private void btnRightClick_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    e.Handled = true;

On right mouse down, I am setting e.Handled = true. This ensures that this will not show up the default Silverlight context menu, and the right mouse up implementation will popup the customized context menu at the current mouse position.

What next? Download the sample application and implement your own logic to create the customized context menu which will open on right click on your Silverlight application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Christian Amado27-Jul-12 13:05
memberChristian Amado27-Jul-12 13:05 
GeneralNice article Pin
Punit_chauhan20-May-10 22:41
memberPunit_chauhan20-May-10 22:41 
ive tried your example but the right click menuis changing position in silverlight full screen mode..
GeneralRe: Nice article Pin
KunalChowdhury21-Sep-10 6:27
mentorKunalChowdhury21-Sep-10 6:27 
GeneralThanks for posting this article Pin
Ziad J.khan22-Apr-10 23:51
memberZiad J.khan22-Apr-10 23:51 
GeneralRe: Thanks for posting this article Pin
KunalChowdhury21-Sep-10 6:25
mentorKunalChowdhury21-Sep-10 6:25 
GeneralRe: Thanks for posting this article Pin
egyamado3-Jan-11 1:21
memberegyamado3-Jan-11 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 28 Nov 2009
Article Copyright 2009 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid