`
wyf
  • 浏览: 433022 次
  • 性别: Icon_minigender_1
  • 来自: 唐山
社区版块
存档分类
最新评论

Silverlight拖放封装

阅读更多

public static class ExtendMethords
    {
        /// <summary>
        /// 核心拖动扩展方法
        /// </summary>
        /// <param name="dragTarget">被拖动的对象</param>
        /// <param name="dragElement">可用鼠标拖拽的部分</param>
        /// <param name="bound">被拖动对象可移动的范围</param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget, FrameworkElement dragElement, DragBound bound)
        {
            bool result = false;
            if (dragTarget.Parent != null && (dragTarget.Parent as Canvas) != null)
            {
                Canvas moveCvs = dragTarget.Parent as Canvas;
                Point downPosition = new Point();
                Point ParentPosition = new Point();
                bool isDrag = false;
                DragBoundCheck check = new DragBoundCheck(dragTarget, bound);
                dragElement.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
                {
                    IDrag dragObj = dragTarget as IDrag;
                    if (dragObj != null && dragObj.IsDrag == false)
                    {
                        return;
                    }
                    if (dragElement.CaptureMouse())
                    {
                        downPosition = e.GetPosition(dragElement);
                        Point mouseOnParentPosition = e.GetPosition(moveCvs);
                        Point mouseOnSlPosition = e.GetPosition(null);
                        ParentPosition = mouseOnSlPosition.Sub(mouseOnParentPosition);
                        isDrag = true;
                    }
                };
                dragElement.MouseMove += delegate(object sender, MouseEventArgs e)
                {
                    if (isDrag)
                    {
                        Point mouseOnSlPosition = e.GetPosition(null);
                        Point targetOnSlPosition = mouseOnSlPosition.Sub(downPosition);
                        targetOnSlPosition = check.GetInBoundPosition(targetOnSlPosition);
                        Point targetOnParentPosition = targetOnSlPosition.Sub(ParentPosition);
                        dragTarget.SetPosition(targetOnParentPosition);

                    }
                };
                dragElement.MouseLeftButtonUp += delegate(object sender, MouseButtonEventArgs e)
                {
                    if (isDrag)
                    {
                        isDrag = false;
                        dragElement.ReleaseMouseCapture();
                    }
                };
                result = true;
            }
            return result;
        }
        /// <summary>
        /// 拖动扩展方法,可以被到处拖动
        /// </summary>
        /// <param name="dragTarget">被拖动的对象</param>
        /// <param name="dragElement">用鼠标拖拽的部分</param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget, FrameworkElement dragElement)
        {
            DragBound bound = new DragBound();
            return BindDrag(dragTarget, dragElement, bound);
        }
        /// <summary>
        /// 拖动扩展方法,可用鼠标拖拽的部分就是被拖动对象本身
        /// </summary>
        /// <param name="dragTarget"></param>
        /// <param name="bound"></param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget, DragBound bound)
        {

            return BindDrag(dragTarget, dragTarget, bound);
        }
        /// <summary>
        /// 没有任何限定的拖动方法
        /// </summary>
        /// <param name="dragTarget"></param>
        /// <returns></returns>
        public static bool BindDrag(this FrameworkElement dragTarget)
        {
            DragBound bound = new DragBound();
            return BindDrag(dragTarget, bound);
        }

         //一下几个函数比较简单,大家一看就明白,就不写注释了,呵呵。
        public static void SetLeft(this FrameworkElement element,double leftPosition)
        {
            element.SetValue(Canvas.LeftProperty, leftPosition);
        }
        public static void SetTop(this FrameworkElement element, double topPosition)
        {
            element.SetValue(Canvas.TopProperty, topPosition);
        }
        public static void SetPosition(this FrameworkElement element, Point position)
        {
            element.SetLeft(position.X);
            element.SetTop(position.Y);
        }

        public static Point Sub(this Point selfPoint, Point subPoint)
        {
            Point result = new Point(selfPoint.X - subPoint.X, selfPoint.Y - subPoint.Y);
            return result;
        }
        public static Point Add(this Point selfPoint, Point addPoint)
        {
            Point result = new Point(selfPoint.X + addPoint.X, selfPoint.Y + addPoint.Y);
            return result;
        }
    
    }

      //函数里面有这么几个类,逻辑也很简单,就直接贴在这里了
      

      public interface IDrag
    {
        bool IsDrag { set; get; }
    }
    /// <summary>
    /// 检测位置是否在设定的范围内
    /// </summary>
    internal class DragBoundCheck
    {
        private FrameworkElement element;
        private DragBound bound;
        public DragBoundCheck(FrameworkElement element,DragBound bound)
        {
                this.element = element;
                this.bound = bound;
        }

        public Point GetInBoundPosition(Point newPosition)
        {
         
            if (newPosition.X > bound.Right-element.ActualWidth)
            {
                newPosition.X = bound.Right - element.ActualWidth;
            }
            if (newPosition.X < bound.Left)
            {
                newPosition.X = bound.Left;
            }
            if (newPosition.Y < bound.Top)
            {
                newPosition.Y = bound.Top;
            }
            if (newPosition.Y > bound.Bottom-element.ActualHeight)
            {
                newPosition.Y = bound.Bottom - element.ActualHeight;
            }
            return newPosition;
        }
    }
    /// <summary>
    ///  设定拖动的范围
    /// </summary>
    public class DragBound
    {
        public double Top { set; get; }
        public double Bottom
        {
            set;
            get;
        }
        public double Left { set; get; }
        public double Right
        {
            set;
            get;
        }
        public DragBound()
        {
            Top = 0.0;
            Left = 0.0;
            //Right = (double)System.Windows.Browser.HtmlPage.Window.Eval("window");
            //Bottom = (double)System.Windows.Browser.HtmlPage.Window.Eval("window");
            Right = double.MaxValue;
            Bottom = double.MaxValue;
        }
    }

      //大家如果觉得那里有问题 ,请指正吧
 
分享到:
评论

相关推荐

    silverlight 拖放操作与拖放行为代码

    在Silverlight应用开发中,拖放操作(Drag-and-Drop)是一种常见的用户交互方式,它允许用户通过鼠标或触摸设备将元素从一个位置移动到另一个位置。本篇将深入探讨Silverlight中的拖放操作和拖放行为代码实现,以及...

    用于业务应用程序的Silverlight拖放

    拖动源是用户点击并移动的对象,放置目标是接收拖动对象的位置,而数据对象则封装了在拖放过程中传递的数据。 ### 2. XAML声明拖放行为 在XAML中,可以通过设置`AllowDrop`属性为`true`使一个元素成为放置目标,并...

    silverlight图片拖拽的效果

    在本文中,我们将深入探讨Silverlight中的图片拖放效果,这是一种强大的交互式UI设计技术,可以为用户带来更丰富的体验。Silverlight是微软推出的一种基于XAML的RIA(富因特网应用程序)开发平台,它提供了丰富的...

    一步一步学Silverlight 2系列

    Silverlight 2系列(8):使用样式封装控件观感 Silverlight 2系列(7):全屏模式支持 Silverlight 2系列(6):键盘事件处理 Silverlight 2系列(5):实现简单的拖放功能 Silverlight 2系列(4):鼠标事件处理 ...

    一步一步学Silverlight_2系列

    Silverlight 2系列(8):使用样式封装控件观感 Silverlight 2系列(7):全屏模式支持 Silverlight 2系列(6):键盘事件处理 Silverlight 2系列(5):实现简单的拖放功能 Silverlight 2系列(4):鼠标事件处理 ...

    Silverlight拖拉控件管理器2.0

    而"Silverlight拖拉控件管理器2.0"就是为了解决这个问题,它提供了封装好的API和事件处理机制,让开发者可以快速集成拖放功能。 **核心特性** 1. **易用性**:该管理器提供了一套简洁的接口,使得开发者可以通过...

    Silverlight入门教程.pdf

    - 封装常用逻辑和界面为可复用组件。 - 在不同场景中重复使用。 #### 七、数据绑定 - **基本概念**: - 绑定路径、数据源、转换器等。 - **双向绑定**: - 实现实时数据更新。 - 如何避免无限循环。 #### 八...

    silverlight4 文件支持拖拽上传

    2. **DataObject**:在Silverlight中,拖放操作涉及到的数据通常封装在`DataObject`对象中。当用户拖动文件时,`DataObject`包含了被拖动文件的信息。 3. **Permission安全模型**:由于安全考虑,Silverlight应用...

    Silverlight 制作可拖动的自定义控件

    - **拖放视觉反馈**:在鼠标移动时改变控件的外观,例如添加半透明遮罩或改变边框颜色,以显示拖动状态。 - **工具提示**(SLMoveRecAndToolTip可能涉及):在拖动过程中显示工具提示,显示控件当前的位置坐标或其他...

    silverlight一步步学习

    创建一个基本的Silverlight应用;2.基本控件 ;3.界面布局;4.鼠标事件处理;5.实现简单的拖放功能;6.键盘事件处理;7.全屏模式支持;8.使用样式封装控件观感;9.使用控件模板;10.使用用户控件;11.数据绑定;12....

    silverlight 4 相關書籍

    - **增强的浏览器集成**:Silverlight 4 提供了更好的浏览器集成,如拖放功能、打印支持以及对桌面应用的模拟,如剪贴板交互。 - **丰富的控件集**:新增和改进的控件如DataGrid、RichTextBox,提供了更多样的界面...

    RadControls for Silverlight5 Q2 2013.2.0611 开发版

    4. **控件集**: 在"RadControls for Silverlight5 Q2 2013.2.0611"中,开发者可以找到一系列预封装的控件,如表格(Grid)、树视图(TreeView)、图表(Chart)、下拉列表(ComboBox)、日期选择器(DatePicker)等...

    DragObject

    总的来说,`DragObject`是Silverlight中实现拖放功能的关键组件,它封装了拖动元素的相关信息和操作逻辑,使得拖放操作变得简单易用。通过对`DragObject`的理解和实践,开发者可以为用户创造更直观、更富于互动性的...

    Telerik_UI_for_Silverlight_2019_1_220_Dev_Downloadly.ir.msi.rar

    在描述中提到的“Telerik_UI_for_Silverlight_2019_1_220_Dev_Downloadly.ir.msi.rar”是一个压缩包文件,它封装了Telerik UI for Silverlight的开发者版本(Dev)的安装程序(.msi)。.msi文件是Microsoft ...

    Silverlight 2教程

    实现简单的拖放功能** - **拖动操作**:实现拖动的基本步骤包括开始拖动、执行拖动、结束拖动。 - **放置操作**:在目标位置接收拖动的数据并执行相应的动作。 **6. 键盘事件处理** - **事件监听**:通过监听键盘...

    HTML5参考手册、jQuery_CHM_1.4.4、arcgis api for silverlight

    此外,jQuery还提供了对Ajax请求的封装,使异步数据交互变得更加简单。 ArcGIS API for Silverlight则是Esri公司提供的一个开发工具,用于构建基于Silverlight技术的地理信息系统应用。Silverlight是微软推出的一种...

    Expression Blend和Silverlight中的相框控件

    Expression Blend提供了直观的拖放界面,以及丰富的设计和动画功能,使得无需编写大量代码就能实现复杂的设计效果。在这个过程中,开发者将学习控件模板、数据绑定、事件处理等关键概念。 标签中的"C#"表明我们将...

Global site tag (gtag.js) - Google Analytics