{
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控件的拖放 *** 作了。希望对大家有所帮助~! 总结
以上是内存溢出为你收集整理的兼容Silverlight4的实用的Silverlight可拖放工具类源代码全部内容,希望文章能够帮你解决兼容Silverlight4的实用的Silverlight可拖放工具类源代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)