Silverlight right-click menu management

Source: Internet
Author: User
Tags silverlight

When Silverlight is full-screen, keyboard events do not respond. At this time, you can only right-click the menu to complete certain operations.

Three methods are introduced in the blog jianyi7659 of csdn on the Internet:

Use the mouserightbuttondown and mouserightbuttonup events provided by sliverlight4.0 or later

Note: In versions earlier than silerlight4.0, right-click events are not supported. For details, see

Right-click the event and only left-click the event. In silerlight4.0 or a later version, you can see that the right-click event is supported.

//
// Summary:
// Occurs when the mouse (or pen) enters the boundary area of system. Windows. uielement.
Public event mouseeventhandler mouseenter;
//
// Summary:
// Occurs when the mouse (or pen) leaves the boundary area of system. Windows. uielement.
Public event mouseeventhandler mouseleave;
//
// Summary:
// It occurs when you press the left mouse button (or the pen tip contacts tablet) and the mouse pointer is hovering over system. Windows. uielement.
Public event mousebuttoneventhandler mouseleftbuttondown;
//
// Summary:
// When the mouse (or pen) hovers over system. Windows. uielement (or system. Windows. uielement has Mouse capture) and the user releases the left mouse button (or
// Occurs when the pen tip is removed from the tablet.
Public event mousebuttoneventhandler mouseleftbuttonup;
//
// Summary:
// When the coordinates of the mouse (or pen) are changed and hovering over system. Windows. uielement (or system. Windows. uielement)
// With Mouse capture.
Public event mouseeventhandler mousemove;
//
// Summary:
// This occurs when the mouse pointer is on system. Windows. uielement and the right mouse button is clicked.
Public event mousebuttoneventhandler mouserightbuttondown;
//
// Summary:
// This occurs when the mouse pointer is located on system. Windows. uielement and the right mouse button is released. However, only system. Windows. uielement. mouserightbuttondown before the caller
// This event is triggered only when it is marked as "handled". For more information, see "Remarks ".
Public event mousebuttoneventhandler mouserightbuttonup;
//
// Summary:
// When the mouse pointer is hovering over system. Windows. uielement or the system. Windows. uielement has focus, you can scroll the scroll wheel of the mouse.
Public event mousewheeleventhandler mousewheel;

Alas, however, in the background code of vs2008, intelligent prompts can be used to right-click the event, but cannot pass during compilation.

You have to upgrade it to silerlight5.0, but vs2008 does not support the silerlight version number when creating it, and the version number cannot be changed in the project.

What's important is that silerlight cannot be used in vs2008, and there is no smart prompt. There is no way to use vs2010. It's still 2010 cool.

First, right-click the menu

You need to write a base class for processing menus when you right-click a menu. This base class processing class is mainly responsible for pop-up, locating, and closing of right-click menus.
 

public abstract class ContextMenu
{
private Point _location;
private bool _isShowing;
private Popup _popup;
private Grid _grid;
private Canvas _canvas;
private FrameworkElement _content;

public void Show(Point location)
{
if (_isShowing)
throw new InvalidOperationException();
_isShowing = true;
_location = location;
ConstunctPopup();
_popup.IsOpen = true;
}

public void Close()
{
_isShowing = false;
if (_popup != null)
_popup.IsOpen = false;
}

protected abstract FrameworkElement GetContent();

protected virtual void OnClickOutside()
{
Close();
}

private void ConstunctPopup()
{
if (_popup != null)
return;
_popup = new Popup();
_grid = new Grid();
_popup.Child = _grid;
_canvas = new Canvas();
_canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };
_canvas.MouseRightButtonDown += (sender, args) => { args.Handled = true; OnClickOutside(); };
_canvas.Background = new SolidColorBrush(Colors.Transparent);
_grid.Children.Add(_canvas);
_content = GetContent();
_content.HorizontalAlignment = HorizontalAlignment.Left;
_content.VerticalAlignment = VerticalAlignment.Top;
_content.Margin = new Thickness(_location.X, _location.Y, 0, 0);
_grid.Children.Add(_content);
UpdateSize();
}

private void UpdateSize()
{
_grid.Width = Application.Current.Host.Content.ActualWidth;
_grid.Height = Application.Current.Host.Content.ActualHeight;

if (_canvas != null)
{
_canvas.Width = _grid.Width;
_canvas.Height = _grid.Height;
}
}
}

II Implementation of right-click menu interface

We have constructed a processing base class for right-click menus. Now let's write a right-click menu interface!

Public class wfcontextmenu: contextmenu
{
Workflow. element. nodes _ nodes;

Public wfcontextmenu (workflow. element. nodes node)
{
_ Nodes = node;
}

Protected override frameworkelement getcontent ()
{
Border border = new border () {borderbrush = new solidcolorbrush (color. fromargb (255,167,171,176), borderthickness = New thickness (1), Background = new solidcolorbrush (colors. White )};
Border. effect = new dropshadoweffect () {blurradius = 3, color = color. fromargb (255,230,227,236 )};
Border. mouserightbuttondown + = (sender, argS) =>{ args. Handled = true ;};

Grid grid = new grid () {margin = New thickness (1 )};
Grid. columndefinitions. Add (New columndefinition () {width = new gridlength (80 )});

// Modify
Button modifybutton = new button () {Height = 22, margin = New thickness (0, 0, 0), verticalignment = verticalignment. Top };
Modifybutton. Click + = modify_mouseleftbuttonup;
Modifybutton. content = "modify ";
Grid. Children. Add (modifybutton );

// Delete
Button deletebutton = new button () {Height = 22, margin = New thickness (0, 24, 0, 0), verticalignment = verticalignment. Top };
Deletebutton. Click + = delete_mouseleftbuttonup;
Deletebutton. content = "delete ";
Grid. Children. Add (deletebutton );

Border. Child = grid;
Return border;
}

Void modify_mouseleftbuttonup (Object sender, routedeventargs E)
{
Workflow. Control. nodesset. showobject (_ nodes );
Close ();
}

Void delete_mouseleftbuttonup (Object sender, routedeventargs E)
{
If (system. Windows. browser. htmlpage. Window. Confirm ("are you sure you want to delete the selected node? "))
{
Int S = _ nodes. Container. nodeslist. count;
For (INT I = 0; I <s; I ++)
{
If (_ nodes. Container. nodeslist [I]. isselect)
{
_ Nodes. Container. removenodes (_ nodes. Container. nodeslist [I]);
S --;
I =-1;
}
}
Close ();
}
}
}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.