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) { 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)); } this.Children.Add(newItem); //update selection foreach (ISelectable item in this.SelectedItems) { item.IsSelected = false; } SelectedItems.Clear(); newItem.IsSelected = true; this.SelectedItems.Add(newItem); } e.Handled = true; } }
void DesignerItem_Loaded(object sender, RoutedEventArgs e) { // if DragThumbTemplate and ConnectorDecoratorTemplate properties of this class // are set these templates are applied; // Note: this method is only executed when the Loaded event is fired, so // setting DragThumbTemplate or ConnectorDecoratorTemplate properties after // will have no effect. if (Template != null) { ContentPresenter contentPresenter = Template.FindName("PART_ContentPresenter", this) as ContentPresenter; if (contentPresenter != null) { UIElement contentVisual = VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement; if (contentVisual != null) { DragThumb thumb = Template.FindName("PART_DragThumb", this) as DragThumb; Control connectorDecorator = Template.FindName("PART_ConnectorDecorator", this) as Control; if (thumb != null) { ControlTemplate template = DesignerItem.GetDragThumbTemplate(contentVisual) as ControlTemplate; if (template != null) { thumb.Template = template; } } if (connectorDecorator != null) { ControlTemplate template = DesignerItem.GetConnectorDecoratorTemplate(contentVisual) as ControlTemplate; if (template != null) { connectorDecorator.Template = template; } } } } } }