Right Click in Silverlight 4

1 minute read

Let's discuss yet another feature of Silverlight 4 - right click! This was most wanted feature and Microsoft has really blessed the Silverlight world by giving this feature!

Our solution will display a red colored box. User can right click within the area of the red box to customize it.  On right click, a pop up will be displayed which will allow the user to change the width & height of the red box. This is the simplest example to demonstrate right click in Silverlight 4

The design part (XAML):-

[sourcecode language='xml']












[/sourcecode]

The C# code for this XAML is:

[sourcecode language='csharp']
namespace SilverlightRightClick
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

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

private void frame_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
pop.HorizontalOffset = e.GetPosition(null).X + 2;
pop.VerticalOffset = e.GetPosition(null).Y + 2;
pop.IsOpen = true;
}

private void slheight_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
if (frame == null) return;
frame.Height = slheight.Value;
}

private void slWidth_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
if (frame == null) return;
frame.Width = slWidth.Value;
}

private void btnDone_Click(object sender, RoutedEventArgs e)
{
pop.IsOpen = false;
}
}
}
[/sourcecode]

Just execute this code and right click on the red colored box!

Updated: