Practical Silverlight drag-and-drop tool source code compatible with silverlight4

Source: Internet
Author: User

Develop daily Silverlight applicationsProgramYou often need to implement drag-and-drop mouse operations on multiple controls in a single domain. The drag-and-drop function in Silverlight is actually very simple, but in order to improve program functionsCodeProgrammers often like to encapsulate common code into a tool class. For example, the sqlhelper class commonly used in Asp.net is used to operate databases, the class we introduce here is the tool class for implementing drag in Silverlight. It supports various versions from silverlight2.0 to silverlight4.0 for general purpose. Well, let's look at the Code:

 Public   Static   Class Dragdrop { Private   Static   Bool Isdragging = False ; Private   Static Point curpoint; Private  Const   Int Max_zindex = 99999; Private   Const   Double Current_opacity = 0.5; Private   Static   Int Lastzindex; Private   Static   Double Lastopacity; Private   Static   Void Sender_mouseleftbuttondown (Object Sender, mousebuttoneventargs e) {uielement = sender As Uielement; If (Uielement! = Null ) {Uielement. capturemouse (); lastzindex = ( Int ) Uielement. getvalue (canvas. zindexproperty); uielement. setvalue (canvas. zindexproperty, max_zindex); lastopacity = uielement. opacity; uielement. Opacity = current_opacity; isdragging = True ; Curpoint = New Point (E. getposition ( Null ). X, E. getposition ( Null ). Y );}} Private   Static   Void Sender_mousemove ( Object Sender, mouseeventargs e ){ If (! Isdragging ){ Return ;} Uielement = sender As Uielement; If (Uielement! = Null ){ Double Currentleft = (Double ) Uielement. getvalue (canvas. leftproperty ); Double Currenttop = ( Double ) Uielement. getvalue (canvas. topproperty ); Double Newleft = ( Double ) Currentleft + E. getposition ( Null ). X-curpoint. X; Double Newtop = ( Double ) Currenttop + E. getposition ( Null ). Y-curpoint. Y; uielement. setvalue (canvas. leftproperty, newleft); uielement. setvalue (canvas. topproperty, newtop); curpoint = New Point (E. getposition ( Null ). X, E. getposition ( Null ). Y );}} Private   Static   Void Sender_mouseleftbuttonup ( Object Sender, mousebuttoneventargs e) {uielement = sender As Uielement; If (Uielement! =Null ) {Uielement. releasemousecapture (); isdragging = False ; Uielement. setvalue (canvas. zindexproperty, lastzindex); uielement. Opacity = lastopacity ;}} Public   Static   Void Load (uielement sender) {sender. mouseleftbuttondown + = New Mousebuttoneventhandler (sender_mouseleftbuttondown); Sender. mouseleftbuttonup + = New Mousebuttoneventhandler (sender_mouseleftbuttonup); Sender. mousemove + = New Mouseeventhandler (sender_mousemove );} Public   Static   Void Unload (uielement sender) {sender. mouseleftbuttondown-= New Mousebuttoneventhandler (sender_mouseleftbuttondown); Sender. mouseleftbuttonup-= New Mousebuttoneventhandler (sender_mouseleftbuttonup); Sender. mousemove-= New Mouseeventhandler (sender_mousemove) ;}} usage of the dragdrop tool class:
 
Dragdrop. Load (layoutroot );
 
Dragdrop is a static class, which is very simple to use. You can drag and drop the Grid Control with only one line of code above.
 
Hope to help you ~!

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.