Silverlight 4.0 feature #1: Right click Mouse events [Context Menu]



Objective

This article will give an introduction of mouse right click events on Silverlight controls.  Silverlight 4.0 introduces context menu for all the controls.

Right Click Mouse Event is added newly added feature in Silverlight 4.0.

Design

Create a Button on Silverlight page.

<
Grid x:Name="LayoutRoot" Background="Black">
<Button x:Name="myButton" Width="75" Height="40" Background="Red" Content="Click Here" />  
</Grid>

Adding Right click events

Now add mouse right click events to button.  Context menu or Right click event could be added to any control in  Silverlight 4.0

There are two Mouse right click events. 

  1. MouseRightButtonDown
  2. MouseRightButtonUp

1.gif

So after adding the events

myButton.MouseRightButtonDown += new MouseButtonEventHandler(mosuedown)
myButton .MouseRightButtonUp  +=new MouseButtonEventHandler(mouseup);

We could handle the event according to our need.

void
mouseup(object sender, MouseButtonEventArgs e)
{
     // Handle the event as of need
}
void mosuedown(object sender, MouseButtonEventArgs e)
{
     // Handle the event as of need
}

While running above code, we will get below output. Once right click on button, default context of Silverlight will come.  See the images below. 

2.gif

On clicking on Silverlight, Silverlight configuration window will get open.  See the image below.

3.gif

Disabling default context of Silverlight
We need to tell Silverlight that context has been handled by making handled as True.

void
mosuedown(object sender, MouseButtonEventArgs e)
{
     e.Handled = true;
}

After enabling handled true the default context of Silverlight will be disabled.   Now if you run the application, you won't get default Silverlight context on right click.

Now, adding a message box to handle the events.

Complete code is as below

using
System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; 

namespace TestingRightClickEvents

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

 

            myButton.MouseRightButtonDown += new MouseButtonEventHandler(mosuedown);

            myButton .MouseRightButtonUp  +=new MouseButtonEventHandler(mouseup);

 

        } 

        void mouseup(object sender, MouseButtonEventArgs e)

        {

            MessageBox.Show("Right Click of Button is Up ");           

        } 

        void mosuedown(object sender, MouseButtonEventArgs e)

        {          

            e.Handled = true;

        }

    }
}

Output

4.gif

5.gif

Conclusion

In this article, I introduced Context menu or Mouse Right click event handling in Silverlight 4.0 Thanks for reading.

 

Up Next
    Ebook Download
    View all
    Learn
    View all