Exemple #1
0
        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            if (IsMouseCaptured)
            {
                ReleaseMouseCapture();
            }
            var adornerLayer = AdornerLayer.GetAdornerLayer(_itemsPanel);

            adornerLayer?.Remove(this);

            if (_drawableInstance == null)
            {
                return;
            }

            _drawableInstance.Y2 = _endPoint.Value.Y;
            _drawableInstance.X2 = _endPoint.Value.X;

            _drawableInstance.Left = Math.Min(_drawableInstance.X1,
                                              _drawableInstance.X2);
            _drawableInstance.Top = Math.Min(_drawableInstance.Y1,
                                             _drawableInstance.Y2);

            var width  = Math.Abs(_drawableInstance.X1 - _drawableInstance.X2);
            var height = Math.Abs(_drawableInstance.Y1 - _drawableInstance.Y2);

            CreateDrawableInstance(width, height);
            _designerPanel.AddItem(_drawableInstance);
            _designerPanel.SelectionService.SelectItem(_drawableInstance);

            e.Handled = true;
        }
        private static void OnDesignerDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetData(typeof(DragObject)) is DragObject dragObject &&
                dragObject.DataContextType !=
                null)
            {
                if (Activator.CreateInstance(dragObject.DataContextType) is IDesignerItem element)
                {
                    Point position = e.GetPosition(_itemsPanel);
                    element.Parent          = _designerViewModel;
                    element.ZIndex          = _designerViewModel.Items.Count;
                    element.TransformOrigin = new Point(0.5, 0.5);
                    element.MinHeight       = 10;
                    element.MinWidth        = 10;
                    if (dragObject.DesiredSize.HasValue)
                    {
                        Size desiredSize = dragObject.DesiredSize.Value;
                        element.Width  = desiredSize.Width;
                        element.Height = desiredSize.Height;

                        element.Left = Math.Max(0,
                                                position.X -
                                                element.Width /
                                                2);
                        element.Top = Math.Max(0,
                                               position.Y -
                                               element.Height /
                                               2);
                    }
                    else
                    {
                        element.Left = Math.Max(0, position.X);
                        element.Top  = Math.Max(0, position.Y);
                    }

                    _designerViewModel.AddItem(element);
                    _designerViewModel.SelectionService.SelectItem(element);
                }
            }

            e.Handled = true;
        }