相容Silverlight4的實用的Silverlight可拖放工具類原始碼

來源:互聯網
上載者:User

開發日常的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控制項的拖放操作了。
希望對大家有所協助~!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.