兼容Silverlight4的实用的Silverlight可拖放工具类源代码
生活随笔
收集整理的這篇文章主要介紹了
兼容Silverlight4的实用的Silverlight可拖放工具类源代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
開發日常的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控件的拖放操作了。
希望對大家有所幫助~!
轉載于:https://www.cnblogs.com/slteam/archive/2010/03/18/1689384.html
總結
以上是生活随笔為你收集整理的兼容Silverlight4的实用的Silverlight可拖放工具类源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 找一部韩国电视剧还是电影,开头女主让一个
- 下一篇: 请一个陪护去医院照顾一天无法自理的病人多