protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            DesignerCanvas designer = VisualTreeHelper.GetParent(this) as DesignerCanvas;

            if (designer != null)
            {
                double h         = designer.ActualHeight / designer.MyRows;
                double w         = designer.ActualWidth / designer.MyCols;
                double left      = DesignerCanvas.GetLeft(this);
                double top       = DesignerCanvas.GetTop(this);
                double newleft   = System.Math.Floor(left / w) * w;
                double newtop    = System.Math.Floor(top / h) * h;
                double newright  = System.Math.Ceiling((left + this.ActualWidth) / w) * w;
                double newbottom = System.Math.Ceiling((top + this.ActualHeight) / h) * h;
                DesignerCanvas.SetLeft(this, newleft);
                DesignerCanvas.SetTop(this, newtop);
                this.Width  = newright - newleft;
                this.Height = newbottom - newtop;
            }
        }
Ejemplo n.º 2
0
        private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (this.designerItem != null && this.designerCanvas != null && this.designerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop = double.MaxValue;
                double minDeltaHorizontal = double.MaxValue;
                double minDeltaVertical = double.MaxValue;
                double dragDeltaVertical, dragDeltaHorizontal;

                foreach (DesignerItem item in this.designerCanvas.SelectedItems)
                {
                    minLeft = Math.Min(Canvas.GetLeft(item), minLeft);
                    minTop  = Math.Min(Canvas.GetTop(item), minTop);

                    minDeltaVertical   = Math.Min(minDeltaVertical, item.ActualHeight - item.MinHeight);
                    minDeltaHorizontal = Math.Min(minDeltaHorizontal, item.ActualWidth - item.MinWidth);
                }

                foreach (DesignerItem item in this.designerCanvas.SelectedItems)
                {
                    switch (VerticalAlignment)
                    {
                    case VerticalAlignment.Bottom:
                        dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
                        double h = item.ActualHeight - dragDeltaVertical;
                        if (DesignerCanvas.GetTop(item) + h < this.designerCanvas.ActualHeight)
                        {
                            item.Height = h;
                        }
                        break;

                    case VerticalAlignment.Top:
                        dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
                        Canvas.SetTop(item, Canvas.GetTop(item) + dragDeltaVertical);
                        item.Height = item.ActualHeight - dragDeltaVertical;
                        break;
                    }

                    switch (HorizontalAlignment)
                    {
                    case HorizontalAlignment.Left:
                        dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
                        Canvas.SetLeft(item, Canvas.GetLeft(item) + dragDeltaHorizontal);
                        item.Width = item.ActualWidth - dragDeltaHorizontal;
                        break;

                    case HorizontalAlignment.Right:
                        dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
                        double w = item.ActualWidth - dragDeltaHorizontal;
                        if (DesignerCanvas.GetLeft(item) + w < this.designerCanvas.ActualWidth)
                        {
                            item.Width = w;
                        }
                        break;
                    }
                }

                e.Handled = true;
            }
        }