Software Architecture-Silverlight 4.0-New Feature- right click as MouseRightButtonDown and MouseRightButtonUp events

In Silverlight 4.0, Microsoft has 2 new events for right click as MouseRightButtonDown and MouseRightButtonUp events to the base UIElement class.

Let use these events to implement a right click menu on a button.

Assume a button with events MouseRightButtonDown as

<Button Content="Context Menu...." Height="23" HorizontalAlignment="Left" Margin="78,85,0,0" Name="button1" VerticalAlignment="Top" Width="148" MouseRightButtonDown="button1_MouseRightButtonDown"  />

Prepare Right click menu as

private void CreatePopup()

            /// Use StackPanel control as contianer control for context menu.
            StackPanel panel1 = new StackPanel();            
            panel1.Background = new SolidColorBrush(Colors.LightGray);

            // Style a border
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Colors.Black);
            border.BorderThickness = new Thickness(5.0);
           // First Menu item
            TextBlock textblock1 = new TextBlock();
            textblock1.Text = "Popup Menu";
            textblock1.Margin = new Thickness(5.0);
            textblock1.FontSize = 16;
            textblock1.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDownHandler);
            // Second Menu item
            TextBlock textblock2 = new TextBlock();
            textblock2.Text = "New window";
            textblock2.Margin = new Thickness(5.0);
            textblock2.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDownHandler);
            // Third Menu item
            TextBlock textblock3 = new TextBlock();
            textblock3.Text = "Close";
            textblock3.Margin = new Thickness(5.0);
            textblock3.MouseLeftButtonDown += new MouseButtonEventHandler(MouseLeftButtonDownHandler);

            // Add menu item to panel
            border.Child = panel1;

            // Set the Child property of Popup to the border 
            // which contains a stackpanel, textblock and button.
            p.Child = border;

Call this method in constructor of page and declare pop in in page scope as:

// Create the popup object.
            Popup p = new Popup();

Event will look like that.

private void button1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
            p.HorizontalOffset = e.GetPosition(null).X + 10;
            p.VerticalOffset = e.GetPosition(null).Y + 10;
            p.IsOpen = true;
            e.Handled = true;


The output of menu will look like that.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: