/// <summary> /// 从可视树上获取某个元素的外层 DesignerItemContainer。 /// </summary> /// <param name="element"></param> /// <returns></returns> public static DesignerItemContainer GetItemContainer(DependencyObject element) { DesignerItemContainer designerItem = null; DependencyObject parent = element; while (true) { parent = VisualTreeHelper.GetParent(parent); if (parent == null) { break; } designerItem = parent as DesignerItemContainer; if (designerItem != null) { break; } //如果已经找到 DesignerCanvas,则退出。 if (parent is DesignerCanvas) { break; } } return(designerItem); }
private static ConnectorInfo GetConnectorInfo(DesignerItemContainer item, Point relative) { var info = new ConnectorInfo(); info.DesignerItemRect = item.GetRect(); info.Position = RectAlgorithm.GetRectAbsolute(info.DesignerItemRect, relative); return(info); }
void DragThumb_DragDelta(object sender, DragDeltaEventArgs e) { var designerItem = DesignerItemContainer.GetItemContainer(this); if (designerItem != null && designerItem.IsSelected) { var designer = DesignerCanvas.GetOwnerCanvas(designerItem); if (designer != null) { //只移动 DesignerItem var designerItems = designer.SelectedItems.OfType <DesignerItemContainer>(); //以下代码不知何意,暂留。 //double minLeft = double.MaxValue; //double minTop = double.MaxValue; //foreach (var item in designerItems) //{ // double left = GetDouble(item, Canvas.LeftProperty); // double top = GetDouble(item, Canvas.TopProperty); // minLeft = Math.Min(left, minLeft); // minTop = Math.Min(top, minTop); //} //double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange); //double deltaVertical = Math.Max(-minTop, e.VerticalChange); double deltaHorizontal = e.HorizontalChange; double deltaVertical = e.VerticalChange; foreach (var item in designerItems) { double left = GetDouble(item, Canvas.LeftProperty); double top = GetDouble(item, Canvas.TopProperty); Canvas.SetLeft(item, left + deltaHorizontal); Canvas.SetTop(item, top + deltaVertical); } designer.InvalidateMeasure(); e.Handled = true; } } }
internal protected virtual void OnDragLineCompleted(DesignerItemContainer source, DesignerItemContainer sink) { //默认实现如下: ////从 0 开始添加,可以保证连接显示在元素之后。 //this.Children.Insert(0, new Connection //{ // Source = source, // Sink = sink //}); var handler = this.DragLineCompleted; if (handler != null) { handler(this, new CanvasDragLineCompletedEventArgs(source, sink)); } }
void thumbDragThumb_DragStarted(object sender, DragStartedEventArgs e) { this.Cursor = Cursors.Cross; this.HitDesignerItem = null; this._pathGeometry = null; if (sender == _sourceDragThumb) { _startItem = _connection.Sink; _draggingItem = _connection.Source; } else if (sender == _sinkDragThumb) { _startItem = _connection.Source; _draggingItem = _connection.Sink; } }
private void HitTesting(Point hitPoint) { var hitObject = _canvas.InputHitTest(hitPoint) as DependencyObject; while (hitObject != null && hitObject != _connector.ParentDesignerItem) { if (hitObject is DesignerCanvas) { break; } if (hitObject is DesignerItemContainer) { HitDesignerItem = hitObject as DesignerItemContainer; return; } hitObject = VisualTreeHelper.GetParent(hitObject); } HitDesignerItem = null; }
protected override void OnDrop(DragEventArgs e) { base.OnDrop(e); var dragObject = e.Data.GetData(typeof(DragObject)) as DragObject; if (dragObject != null && !String.IsNullOrEmpty(dragObject.Xaml)) { var content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml))); if (content != null) { var newItem = new DesignerItemContainer(); newItem.Content = content; var 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); this.AddSelection(true, newItem); } e.Handled = true; } }
public CanvasDragLineCompletedEventArgs(DesignerItemContainer source, DesignerItemContainer sink) { this.Source = source; this.Sink = sink; }
protected internal virtual void OnDragLineCompleted(DesignerItemContainer source, DesignerItemContainer sink) { //默认实现如下: ////从 0 开始添加,可以保证连接显示在元素之后。 //this.Children.Insert(0, new Connection //{ // Source = source, // Sink = sink //}); var handler = this.DragLineCompleted; if (handler != null) handler(this, new CanvasDragLineCompletedEventArgs(source, sink)); }
private void AddItemIntoCanvas(BlockControl block) { var container = new DesignerItemContainer(); container.Content = block; block.Container = container; SetComponent(container, block); //应用样式 var containerStyle = this.BlockContainerStyle; if (containerStyle != null) container.Style = containerStyle; container.DataContext = block; container.SetBinding(Canvas.LeftProperty, new Binding { Path = new PropertyPath(BlockControl.LeftProperty), Mode = BindingMode.TwoWay }); container.SetBinding(Canvas.TopProperty, new Binding { Path = new PropertyPath(BlockControl.TopProperty), Mode = BindingMode.TwoWay }); _canvas.Children.Add(container); }
private static ConnectorInfo GetConnectorInfo(DesignerItemContainer item, Point relative) { var info = new ConnectorInfo(); info.DesignerItemRect = item.GetRect(); info.Position = RectAlgorithm.GetRectAbsolute(info.DesignerItemRect, relative); return info; }
void thumbDragThumb_DragCompleted(object sender, DragCompletedEventArgs e) { if (_changeTarget) { var hitted = this.HitDesignerItem; if (hitted != null) { if (_connection.Sink == _draggingItem) { _connection.Sink = hitted; } else { _connection.Source = hitted; } this.HitDesignerItem = null; } _connection.StrokeDashArray = _rawStrokeDashArray; _changeTarget = false; } this._pathGeometry = null; this.InvalidateVisual(); }
private void HitTesting(Point hitPoint) { var hitObject = _designerCanvas.InputHitTest(hitPoint) as DependencyObject; while (hitObject != null && hitObject != _startItem) { if (hitObject is DesignerCanvas) break; if (hitObject is DesignerItemContainer) { HitDesignerItem = hitObject as DesignerItemContainer; return; } hitObject = VisualTreeHelper.GetParent(hitObject); } HitDesignerItem = null; }