private static void MyColsProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { DesignerCanvas designerCanvas = (DesignerCanvas)d; int myCols = (int)e.NewValue; if (myCols > 0 && designerCanvas.MyRows > 0 && designerCanvas.ActualHeight > 0 && designerCanvas.ActualWidth > 0) { designerCanvas.BrushGrid(); } }
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; } }
private void DesignerItem_Loaded(object sender, RoutedEventArgs e) { if (this.Template != null) { //ContentPresenter contentPresenter = // this.Template.FindName("PART_ContentPresenter", this) as ContentPresenter; //MoveThumb thumb = // this.Template.FindName("PART_MoveThumb", this) as MoveThumb; MenuItem menu_delete = this.Template.FindName("PART_Delete", this) as MenuItem; if (menu_delete != null) { //delete the DesignerItem menu_delete.Click += new RoutedEventHandler((x, y) => { DesignerCanvas designer = (DesignerCanvas)VisualTreeHelper.GetParent(this); if (designer != null) { designer.Children.Remove(this); } }); } //if (contentPresenter != null && thumb != null) //{ // UIElement contentVisual = // VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement; // if (contentVisual != null) // { // ControlTemplate template = // DesignerItem.GetMoveThumbTemplate(contentVisual) as ControlTemplate; // if (template != null) // { // thumb.Template = template; // } // } //} } }
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; } }
protected override void OnPreviewMouseDown(MouseButtonEventArgs e) { base.OnPreviewMouseDown(e); DesignerCanvas designer = VisualTreeHelper.GetParent(this) as DesignerCanvas; if (designer != null) { if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None) { this.IsSelected = !this.IsSelected; } else { //if (!this.IsSelected) //{ designer.DeselectAll(); this.IsSelected = true; //} } } e.Handled = false; }
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; } }