Esempio n. 1
0
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);
            string xamlString = e.Data.GetData("DESIGNER_ITEM") as string;

            if (!String.IsNullOrEmpty(xamlString))
            {
                DesignerItem newItem = null;
                //FrameworkElement content = XamlReader.Load(XmlReader.Create(new StringReader(xamlString))) as FrameworkElement;
                Button content = new Button()
                {
                    Content = xamlString, Background = Brushes.Gray
                };
                content.IsHitTestVisible = false;
                if (content != null)
                {
                    newItem         = new DesignerItem();
                    newItem.Style   = this.TryFindResource("DesignerItemStyle") as Style;
                    newItem.Content = content;

                    Point position = e.GetPosition(this);
                    //if (content.MinHeight != 0 && content.MinWidth != 0)
                    //{
                    //    newItem.Width = content.MinWidth * 2; ;
                    //    newItem.Height = content.MinHeight * 2;
                    //}
                    //else
                    //{
                    //    newItem.Width = 65;
                    //    newItem.Height = 65;
                    //}
                    double unitwidth  = this.ActualWidth / MyCols;
                    double unitheight = this.ActualHeight / MyRows;
                    newItem.Width  = unitwidth;
                    newItem.Height = unitheight;
                    double left = Math.Floor(position.X / unitwidth) * unitwidth;
                    double top  = Math.Floor(position.Y / unitheight) * unitheight;
                    DesignerCanvas.SetLeft(newItem, Math.Max(0, left /*position.X - newItem.Width / 2*/));
                    DesignerCanvas.SetTop(newItem, Math.Max(0, top /*position.Y - newItem.Height / 2*/));
                    this.Children.Add(newItem);

                    this.DeselectAll();
                    newItem.IsSelected = true;
                }

                e.Handled = true;
            }
        }
        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;
            }
        }