.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Silverlight 4: How to use the all new Right Click Context Menu?

Posted By:Kunal Chowdhury       Posted Date: March 06, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

In this post I will describe about the another cool new feature ("How to use the all new Right Click Context Menu?") of Silverlight 4.


In my previous posts I discussed about "How to work with Notification API?" & "How to Capture Video from Default Webcam?". In this post I will describe about the another cool new feature ("How to use the all new Right Click Context Menu?") of Silverlight 4.

Code Walk-through

Silverlight 4 has now support for right click. You can now register the event "MouseRightButtonDown" & "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;

Now if you want to implement a Context Menu on right click, create the Popup Context menu & position it to proper 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, on click it will show which menu item has been cllicked by you. Upto this I 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 below implemented code which will be 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 the e.Handled = true. This ensures that, this will not show up the default Silverlight context menu & the right mouse up implementation will popup the customized context menu at the current mouse position.


What next? Download the sample application created by me & implement your own logic to create the customized context menu which will open on right click on your silverlight application.

Download Sample Application:  Silverlight 4 Right Click Context Menu Demo

 Subscribe to Articles


Further Readings:

Author: GumbyJ         Company URL: http://www.dotnetspark.com
Posted Date: July 07, 2010


This sample contains a memory leak. Please watch your memory utilization for your browser. You will see it increase each time you open a menu.

The following lines add delegates but they are never removed.

popupCanvas.MouseLeftButtonDown +=
popupCanvas.MouseRightButtonDown +=

I am having problems with the ContextMenu leaking memory myself. When I move the ContextMenu to the designer it removes the memory leak but then the ContextMenu doesn't consistently appear where I want it to.
Author: Kunal Chowdhury         Company URL: http://www.dotnetspark.com
Posted Date: July 07, 2010

Hi GumbyJ,

Thanks for your feedback. Yes, there is a Memory Leak there, but as I was demonstrating a sample application for the context menu, thus I missed it somewhere.

Thanks again for pointing that to me.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend