开发日常的Silverlight应用程序时,常常要对一个域多个控件实现可拖放的MOUSE *** 作,在Silverlight中实现拖放的功能其实非常简单,但是为了提高程序功能代码的可复用性,程序员常常喜欢把常用的代码封装成一个工具类,例如Asp.net中常用sqlHelper类,用来 *** 作数据库的,这里我们介绍的类是在Silverlight中实现拖动的工具类,它支持Silverlight2.0至Silverlight4.0的各个版本通用,好了话不多说,我们还是看代码吧:
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 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 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 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); }}DragDrop工具类的使用方法:
DragDrop.Load(LayoutRoot);
DragDrop是一个静态类,使用起来非常简单,以上只要一行代码就可以实现对GrID控件的拖放 *** 作了。
希望对大家有所帮助~!总结
以上是内存溢出为你收集整理的兼容Silverlight4的实用的Silverlight可拖放工具类源代码全部内容,希望文章能够帮你解决兼容Silverlight4的实用的Silverlight可拖放工具类源代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)