/// <summary> /// 释放拖放 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { var data = Helper.DragDrop.GetData(); if (data is System.Windows.Controls.Primitives.Popup) { var pop = data as System.Windows.Controls.Primitives.Popup; var unit = pop.Child as Elements.Unit; if (unit != null) { pop.Child = null; unit.Position = CanvasGrid.CheckPosition(e.GetPosition(CanvasGrid.BackCanvas)); var el = CanvasGrid.GetElement(unit.Position); //必须是当前位置无元素方可放置 if (!(el is Elements.Unit)) { CanvasGrid.AddElement(unit); BindMenu();//绑定菜单 } } pop.IsOpen = false; Helper.DragDrop.Clear(); StopDrag(); } }
/// <summary> /// 拖放完成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) { if (CanvasGrid == null) { return; } var p = new Point(); p.X = Canvas.GetLeft(this); if (p.X == double.NaN) { p.X = 0; } p.Y = Canvas.GetTop(this); if (p.Y == double.NaN) { p.Y = 0; } CanvasGrid.CheckCanvasGrid(); //var p = new Point(firstClickPosition.X + e.HorizontalChange, firstClickPosition.Y + e.VerticalChange); var newp = CanvasGrid.CheckPosition(p); //如果当前位置已有元素 //则放弃落地 var el = CanvasGrid.GetElement(newp); if (el == null && newp.X != 0) { this.Position = newp; } this.Opacity = 1; //重新定位。 this.ResetPosition(); }