Click here to Skip to main content
6,822,123 members and growing! (17,596 online)
Email Password   helpLost your password?
Web Development » Silverlight » HowTo     Beginner License: The Code Project Open License (CPOL)

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

By KunalChowdhury

This article demonstrates the all new right click feature of Silverlight 4.
C#3.0, C#4.0, Silverlight, Dev
Revision:3 (See All)
Posted:28 Nov 2009
Views:2,068
Bookmarked:8 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.35 Rating: 4.50 out of 5

1

2

3
1 vote, 50.0%
4
1 vote, 50.0%
5

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

KunalChowdhury


Member
He is currently working as a Silverlight application developer. Has a very good skill over C#, XAML, Silverlight & WPF. He has a good working experience in Windows 7 application (including Multitouch) development.

During his professional career he worked in various technologies & delivered quality output. He never hesitates to take up challenges & work on the latest technologies in Microsoft platform.

He attended various software development competition & achieved different awards.

He is presently focusing on the RIA (Silverlight & WPF) & willing to become a Technology Specialist in Microsoft platform. Learning newer things, Blog posting & helping others in forums is one of his regular activity.

Specialties:

Silverlight Application Development, WPF Application Development, Windows 7 Application Development
Occupation: Software Developer
Location: India India

Other popular Silverlight articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 28 Nov 2009
Editor: Smitha Vijayan
Copyright 2009 by KunalChowdhury
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project