using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity;
using Microsoft.Expression.Interactivity.Layout;
using System.Windows.Browser;
namespace testControlMove
{
public partial class Rec : UserControl
{
public double x = 0;
public double y = 0;
public Rec()
{
InitializeComponent();
BindMenu(image1);
}
MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
private bool isDrag;
/// <summary>
/// 是否允许被拖拽和鼠标右键
/// </summary>
public bool IsDrag
{
get { return isDrag; }
set
{
isDrag = value;
if (isDrag == true)
{
dragBehavior.Attach(this); //将本对象加入到可以鼠标拖动的行为对象中去
dragBehavior.DragFinished += new MouseEventHandler(dragBehavior_DragFinished); //在对象移动成功之后加载一个处理事件。
}
else if (isDrag == false)
{
try
{
dragBehavior.Detach(); //设置本控件移动行为取消,并且取消DragFinished处理事件
dragBehavior.DragFinished -= new MouseEventHandler(dragBehavior_DragFinished);
}
catch
{
}
}
}
}
/// <summary>
/// 被拖动完成之后触发的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void dragBehavior_DragFinished(object sender, MouseEventArgs e)
{
MouseDragElementBehavior dragBehavior = sender as MouseDragElementBehavior;
this.Tag = dragBehavior.X + "|" + dragBehavior.Y; // this.Tag设置为相应的值
x = dragBehavior.X;
y = dragBehavior.Y;
}
protected void BindMenu(Image c)
{
ContextMenu cm = new ContextMenu();//新建右键菜单
MenuItem mi = new MenuItem();//新建右键菜单项
mi.Header = "编辑";
mi.Click += MenuItem1_Click;//为菜单项注册事件
cm.Items.Add(mi);
MenuItem mi2 = new MenuItem();//新建右键菜单项
mi2.Header = "删除";
mi2.Click += MenuItem2_Click;//为菜单项注册事件
cm.Items.Add(mi2);
ContextMenuService.SetContextMenu(c, cm);//为控件绑定右键菜单
}
private void MenuItem1_Click(object sender, RoutedEventArgs e)
{
ChildWindow1 child = new ChildWindow1();
child.Show();
}
private void MenuItem2_Click(object sender, RoutedEventArgs e)
{
this.Visibility = Visibility.Collapsed;
}
private void UserControl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
}
}
}