(1)最简单的方法
在需要拖动的控件下加入以下代码就可以实现拖拽,还可以配置相关函数处理拖拽各个阶段事件。
引用部分:
xmlns:i=" http://schemas.microsoft.com/expression/2010/interactivity "
xmlns:ei=" http://schemas.microsoft.com/expression/2010/interactions "
代码部分:
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior DragBegun="onDragBegun" DragFinished="dragFinished" Dragging="onDragging" />
</i:Interaction.Behaviors>
(2)在后台代码中实现,发现有两种方式,至于其原理是什么还有待研究:
方式1:
MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
Interaction.GetBehaviors(this.ContentFrame).Add(dragBehavior);
方式2:
MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
dragBehavior.Attach(this.ContentFrame);
以上两种方法都会覆盖子控件和本控件的鼠标右键下击和右键弹回事件,但是在本控件中还是可以通过以下配置予以实现:
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(ellipse_MouseLeftButtonDown),true);//右键下击
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(ellipse_MouseLeftButtonUp),true);//右键弹回
配置好了之后,鼠标右键下击和右键弹回事件会先于拖拽事件执行,所以在下击时去掉拖拽效果还是来得及的,去掉代码如下所示:
Interaction.GetBehaviors(this.ContentFrame).Clear();
(3)一天的纠结,终于使用到了第三种方法,使用margin这东西来实现拖拽,对布局也木有啥特殊的要求,话不多说,贴点代码记录下:
首先配置好各个事件:
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonDownEvent,true);
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonUpEvent,true);
this.ContentFrame.MouseMove += new MouseEventHandler(ellipse_MouseMove);
//具体实现代码
/// <summary>
/// 标识是否可以拖动对象
/// </summary>
private bool isDrag = false;
在需要拖动的控件下加入以下代码就可以实现拖拽,还可以配置相关函数处理拖拽各个阶段事件。
引用部分:
xmlns:i=" http://schemas.microsoft.com/expression/2010/interactivity "
xmlns:ei=" http://schemas.microsoft.com/expression/2010/interactions "
代码部分:
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior DragBegun="onDragBegun" DragFinished="dragFinished" Dragging="onDragging" />
</i:Interaction.Behaviors>
(2)在后台代码中实现,发现有两种方式,至于其原理是什么还有待研究:
方式1:
MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
Interaction.GetBehaviors(this.ContentFrame).Add(dragBehavior);
方式2:
MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
dragBehavior.Attach(this.ContentFrame);
以上两种方法都会覆盖子控件和本控件的鼠标右键下击和右键弹回事件,但是在本控件中还是可以通过以下配置予以实现:
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(ellipse_MouseLeftButtonDown),true);//右键下击
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(ellipse_MouseLeftButtonUp),true);//右键弹回
配置好了之后,鼠标右键下击和右键弹回事件会先于拖拽事件执行,所以在下击时去掉拖拽效果还是来得及的,去掉代码如下所示:
Interaction.GetBehaviors(this.ContentFrame).Clear();
(3)一天的纠结,终于使用到了第三种方法,使用margin这东西来实现拖拽,对布局也木有啥特殊的要求,话不多说,贴点代码记录下:
首先配置好各个事件:
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonDownEvent,true);
this.ContentFrame.AddHandler(FrameworkElement.MouseLeftButtonUpEvent,true);
this.ContentFrame.MouseMove += new MouseEventHandler(ellipse_MouseMove);
//具体实现代码
/// <summary>
/// 标识是否可以拖动对象
/// </summary>
private bool isDrag = false;
private Point StartPoint;
private Point EndPoint;
//鼠标右键单击事件
private Point EndPoint;
//鼠标右键单击事件
private void ellipse_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
isDrag = true;
StartPoint = e.GetPosition(LayoutRoot);
//ContentFrame.CaptureMouse();在这里是必须去掉的,不然子控件中的事件还是没有反应的!
}
//鼠标移动事件
{
isDrag = true;
StartPoint = e.GetPosition(LayoutRoot);
//ContentFrame.CaptureMouse();在这里是必须去掉的,不然子控件中的事件还是没有反应的!
}
//鼠标移动事件
private void ellipse_MouseMove(object sender,MouseEventArgs e)
{
ContentFrame.CaptureMouse();
if (isDrag)
{
EndPoint = e.GetPosition(LayoutRoot);
{
ContentFrame.CaptureMouse();
if (isDrag)
{
EndPoint = e.GetPosition(LayoutRoot);
//计算X、Y轴起始点与终止点之间的相对偏移量
double y = EndPoint.Y - StartPoint.Y;
double x = EndPoint.X - StartPoint.X;
double y = EndPoint.Y - StartPoint.Y;
double x = EndPoint.X - StartPoint.X;
Thickness margin = ContentFrame.Margin;
//计算新的Margin
Thickness newMargin = new Thickness()
{
Left = margin.Left + x,
Top = margin.Top + y,
Right = margin.Right - x,
Bottom = margin.Bottom - y
};
Thickness newMargin = new Thickness()
{
Left = margin.Left + x,
Top = margin.Top + y,
Right = margin.Right - x,
Bottom = margin.Bottom - y
};
ContentFrame.Margin = newMargin;
StartPoint = EndPoint;
}
}
//鼠标右键弹回事件
}
}
//鼠标右键弹回事件
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。