兼容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控件的拖放操作了。希望对大家有所帮助~!


本文转自dotfun 51CTO博客,原文链接:http://blog.51cto.com/dotfun/286060
相关文章
Silverlight & Blend动画设计系列八:拖放(Drag-Drop)操作与拖放行为(DragBehavior)
原文:Silverlight & Blend动画设计系列八:拖放(Drag-Drop)操作与拖放行为(DragBehavior)   在Silverlight中自身并没有提供拖放功能的相关实现,要实现拖放功能得借助其事件支持(MouseLeftButtonDown、MouseLeftButtonUp和MouseMove)来完成,实际应用中我们可以通过行为(Behavior)特性将拖放操作封装为行为,这样可达到代码复用的效果。
1076 0

热门文章

最新文章

下一篇
无影云桌面