1. MouseRightButtonDown
2. MouseRightButtonUp
Note that in order for the MouseRightButtonUp event to be fired you
must first handle the MouseRightButtonDown event. Thus, it’s important
to monitor for both of these events.
The following code shows a border control with these right click events hooked up:
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas,"Courier New",courier,monospace; background-color: rgb(255, 255, 255); }.csharpcode pre { margin: 0em; }.csharpcode .rem { color: rgb(0, 128, 0); }.csharpcode .kwrd { color: rgb(0, 0, 255); }.csharpcode .str { color: rgb(0, 96, 128); }.csharpcode .op { color: rgb(0, 0, 192); }.csharpcode .preproc { color: rgb(204, 102, 51); }.csharpcode .asp { background-color: rgb(255, 255, 0); }.csharpcode .html { color: rgb(128, 0, 0); }.csharpcode .attr { color: rgb(255, 0, 0); }.csharpcode .alt { background-color: rgb(244, 244, 244); width: 100%; margin: 0em; }.csharpcode .lnum { color: rgb(96, 96, 96); }
Notice a background color must be specified since mouse events are not fired on elements that are transparent or not painted in.
I will also add a Popup control that will act as my context menu. I
set the Visibility=”Collapsed” in order to hide the control until the
right mouse click event occurs. Also, I have placed the Popup control in
a Canvas so that I can easily position it relative to mouse coordinates
on click. The only way to position a control contained in a Grid is
through column/row definitions or margins.
<Canvas>
<Popup x:Name="ContextMenu" Visibility="Collapsed">
<Border CornerRadius="5" BorderBrush="White" BorderThickness="1" Background=”Black”>
<StackPanel >
<HyperlinkButton IsEnabled="True" Visibility="Visible"
Content="Menu Item #1" Foreground="White"/>
<HyperlinkButton Content="Menu Item #2" Foreground="White"/>
<HyperlinkButton Content="Menu Item #3" Foreground="White"/>
<HyperlinkButton Content="Menu Item #3" Foreground="White"/>
</StackPanel>
</Border>
</Popup>
</Canvas>
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas,"Courier New",courier,monospace; background-color: rgb(255, 255, 255); }.csharpcode pre { margin: 0em; }.csharpcode .rem { color: rgb(0, 128, 0); }.csharpcode .kwrd { color: rgb(0, 0, 255); }.csharpcode .str { color: rgb(0, 96, 128); }.csharpcode .op { color: rgb(0, 0, 192); }.csharpcode .preproc { color: rgb(204, 102, 51); }.csharpcode .asp { background-color: rgb(255, 255, 0); }.csharpcode .html { color: rgb(128, 0, 0); }.csharpcode .attr { color: rgb(255, 0, 0); }.csharpcode .alt { background-color: rgb(244, 244, 244); width: 100%; margin: 0em; }.csharpcode .lnum { color: rgb(96, 96, 96); }
Now that you have a control to click on and a context menu to pop up
let’s look at the events that drive opening and closing this context
menu.
To start, set e.Handled=true in MouseRightButtonDown so that the MouseRightButtonUp will also be fired.
private void Border_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas,"Courier New",courier,monospace; background-color: rgb(255, 255, 255); }.csharpcode pre { margin: 0em; }.csharpcode .rem { color: rgb(0, 128, 0); }.csharpcode .kwrd { color: rgb(0, 0, 255); }.csharpcode .str { color: rgb(0, 96, 128); }.csharpcode .op { color: rgb(0, 0, 192); }.csharpcode .preproc { color: rgb(204, 102, 51); }.csharpcode .asp { background-color: rgb(255, 255, 0); }.csharpcode .html { color: rgb(128, 0, 0); }.csharpcode .attr { color: rgb(255, 0, 0); }.csharpcode .alt { background-color: rgb(244, 244, 244); width: 100%; margin: 0em; }.csharpcode .lnum { color: rgb(96, 96, 96); }
In MouseRightButtonUp I am simply get the coordinates of the mouse
click relative to my application. This is also where I then set my Popup
control to Visible and IsOpen = true.
private void Border_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(null);
e.Handled = true;
ContextMenu.Visibility = Visibility.Visible;
ContextMenu.IsOpen = true;
ContextMenu.SetValue(Canvas.LeftProperty, (double)p.X);
ContextMenu.SetValue(Canvas.TopProperty, (double)p.Y);
}
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas,"Courier New",courier,monospace; background-color: rgb(255, 255, 255); }.csharpcode pre { margin: 0em; }.csharpcode .rem { color: rgb(0, 128, 0); }.csharpcode .kwrd { color: rgb(0, 0, 255); }.csharpcode .str { color: rgb(0, 96, 128); }.csharpcode .op { color: rgb(0, 0, 192); }.csharpcode .preproc { color: rgb(204, 102, 51); }.csharpcode .asp { background-color: rgb(255, 255, 0); }.csharpcode .html { color: rgb(128, 0, 0); }.csharpcode .attr { color: rgb(255, 0, 0); }.csharpcode .alt { background-color: rgb(244, 244, 244); width: 100%; margin: 0em; }.csharpcode .lnum { color: rgb(96, 96, 96); }
Finally, I also added a MouseLeftButtonDown event to my Border
control so that I can close the Popup when mouse left occurs as seen
here:
private void BorderCtrl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ContextMenu.Visibility = Visibility.Collapsed;
ContextMenu.IsOpen = false;
}