Click here to Skip to main content
Click here to Skip to main content

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

By , 28 Nov 2009
 

Introduction

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.

Background

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.Children.Add(popupCanvas);
    popupGrid.Children.Add(CreateContextMenuItems(currentMousePosition));

    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);
    ShowPopup(currentMousePosition);
}

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.

License

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

About the Author

_ Kunal Chowdhury _
Software Developer
India India
Member
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberChristian Amado27 Jul '12 - 12:05 
I like your article =) What do you thing about this? My Technical Blog Wink | ;)
GeneralNice articlememberPunit_chauhan20 May '10 - 21:41 
Hi,
ive tried your example but the right click menuis changing position in silverlight full screen mode..
GeneralRe: Nice articlementorKunalChowdhury21 Sep '10 - 5:27 
Hi Punit,
 
Sorry for the late reply as the new Message posted in article forum doesn't send email notification and hence it is difficult to track the comments posted for a huge nos. of articles.
 
BTW, what about the current status of your problem?


Regards - Kunal Chowdhury | Software Developer | Blog | Twitter | Silverlight Tutorial | Indian Forum

GeneralThanks for posting this articlememberZiad J.khan22 Apr '10 - 22:51 
I've adapted your code into a small reusable class and posted it on CodePlex. I added drop shadow and fade in effects as well. Left click and hover events are also supported. Here it is:
http://sl4popupmenu.codeplex.com/[^]
GeneralRe: Thanks for posting this articlementorKunalChowdhury21 Sep '10 - 5:25 
Ziad J.khan wrote:
I've adapted your code into a small reusable class and posted it on CodePlex. I added drop shadow and fade in effects as well. Left click and hover events are also supported. Here it is:
http://sl4popupmenu.codeplex.com/[^]

 
Nice to see your implementation. Have a +5 from me. Keep it up. Thumbs Up | :thumbsup:


Regards - Kunal Chowdhury | Software Developer | Blog | Twitter | Silverlight Tutorial | Indian Forum

GeneralRe: Thanks for posting this articlememberegyamado3 Jan '11 - 0:21 
Hi Ziad,
 
I have been looking for solution to do right click on selected content, which you have it with the TextBox control.
 
But this has down side, which is user can add or delete the content.
 
Is there any way that user can only select (without editing the existing content/text) then after select some text, menu will show up with limited features such as (copy, highlight, print,....) ????!!!!
 
The idea is to protect the text/content that inside Text Box Control?? And if TextBox doesn't work, it there any other control or solution to do that?
 
Thanks

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 28 Nov 2009
Article Copyright 2009 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid