void DragThumb_DragDelta(object sender, DragDeltaEventArgs e) { DesignerItem designerItem = this.DataContext as DesignerItem; DesignerCanvas designer = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas; if (designerItem != null && designer != null && designerItem.IsSelected) { double minLeft = double.MaxValue; double minTop = double.MaxValue; // we only move DesignerItems var designerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>(); foreach (DesignerItem item in designerItems) { double left = Canvas.GetLeft(item); double top = Canvas.GetTop(item); minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft); minTop = double.IsNaN(top) ? 0 : Math.Min(top, minTop); } double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange); double deltaVertical = Math.Max(-minTop, e.VerticalChange); foreach (DesignerItem item in designerItems) { double left = Canvas.GetLeft(item); double top = Canvas.GetTop(item); if (double.IsNaN(left)) { left = 0; } if (double.IsNaN(top)) { top = 0; } Canvas.SetLeft(item, left + deltaHorizontal); Canvas.SetTop(item, top + deltaVertical); } designer.InvalidateMeasure(); e.Handled = true; } }
protected override void OnDrop(DragEventArgs e) { base.OnDrop(e); DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject; if (dragObject != null && !String.IsNullOrEmpty(dragObject.Xaml)) { DesignerItem newItem = null; Object content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml))); if (content != null) { newItem = new DesignerItem(); newItem.Content = content; Point position = e.GetPosition(this); //if (dragObject.DesiredSize.HasValue) if (!double.IsNaN(dragObject.DesiredSize.Value.Width) && !double.IsNaN(dragObject.DesiredSize.Value.Height)) { Size desiredSize = dragObject.DesiredSize.Value; newItem.Width = desiredSize.Width; newItem.Height = desiredSize.Height; DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X - newItem.Width / 2)); DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y - newItem.Height / 2)); } else { DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X)); DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y)); } Canvas.SetZIndex(newItem, this.Children.Count); this.Children.Add(newItem); //update selection this.SelectionService.SelectItem(newItem); newItem.Focus(); } e.Handled = true; } }
void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e) { DesignerItem designerItem = this.DataContext as DesignerItem; DesignerCanvas designer = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas; if (designerItem != null && designer != null && designerItem.IsSelected) { double minLeft, minTop, minDeltaHorizontal, minDeltaVertical; double dragDeltaVertical, dragDeltaHorizontal, scale; IEnumerable <DesignerItem> selectedDesignerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>(); CalculateDragLimits(selectedDesignerItems, out minLeft, out minTop, out minDeltaHorizontal, out minDeltaVertical); foreach (DesignerItem item in selectedDesignerItems) { if (item != null && item.ParentID == Guid.Empty) { switch (base.VerticalAlignment) { case VerticalAlignment.Bottom: dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical); scale = (item.ActualHeight - dragDeltaVertical) / item.ActualHeight; DragBottom(scale, item, designer.SelectionService); break; case VerticalAlignment.Top: double top = Canvas.GetTop(item); dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical); scale = (item.ActualHeight - dragDeltaVertical) / item.ActualHeight; DragTop(scale, item, designer.SelectionService); break; default: break; } switch (base.HorizontalAlignment) { case HorizontalAlignment.Left: double left = Canvas.GetLeft(item); dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal); scale = (item.ActualWidth - dragDeltaHorizontal) / item.ActualWidth; DragLeft(scale, item, designer.SelectionService); break; case HorizontalAlignment.Right: dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal); scale = (item.ActualWidth - dragDeltaHorizontal) / item.ActualWidth; DragRight(scale, item, designer.SelectionService); break; default: break; } } } e.Handled = true; } }
public SelectionService(DesignerCanvas canvas) { this.designerCanvas = canvas; }