diff --git a/AIStudio.Wpf.DiagramDesigner/Controls/ItemsContainer.cs b/AIStudio.Wpf.DiagramDesigner/Controls/ItemsContainer.cs index a2e4ac8..5f2a6de 100644 --- a/AIStudio.Wpf.DiagramDesigner/Controls/ItemsContainer.cs +++ b/AIStudio.Wpf.DiagramDesigner/Controls/ItemsContainer.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Linq; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; @@ -146,11 +147,21 @@ namespace AIStudio.Wpf.DiagramDesigner var currentPoint = e.GetPosition(this); if (Math.Sqrt(Math.Pow(firstPoint.Value.X - currentPoint.X, 2) + Math.Pow(firstPoint.Value.Y - currentPoint.Y, 2)) > 5) { + firstPoint = null; if (Info?.Children?.Count > 0) { var borders = VisualHelper.FindVisualChildren(this); + BlockBorder innerblock = null; + BlockDesignerItemViewModel dragObject = null; + Point dragOffset = new Point(); foreach (var border in borders) { + var itemsContainer = VisualHelper.TryFindParent(border); + if (this != itemsContainer) + { + continue; + } + var point = border.TransformToAncestor(this).Transform(new Point(0, 0)); var rect = new Rect(point.X, point.Y, border.ActualWidth, border.ActualHeight); if (rect.Contains(currentPoint)) @@ -167,10 +178,10 @@ namespace AIStudio.Wpf.DiagramDesigner if (canvas != null) { canvas.SourceItemsContainer = this; + e.Handled = true; } } } - firstPoint = null; } } } @@ -179,7 +190,6 @@ namespace AIStudio.Wpf.DiagramDesigner base.OnMouseUp(e); firstPoint = null; - DragObject = null; } public ConnectorOrientation Orientation diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/ItemsContainerInfo.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/ItemsContainerInfo.cs index bf7fbef..ab294bc 100644 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/ItemsContainerInfo.cs +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/Container/ItemsContainerInfo.cs @@ -227,19 +227,23 @@ namespace AIStudio.Wpf.DiagramDesigner Children.Remove(child); } - public List GetAllContain(ObservableCollection children) + public List GetAllContain(ObservableCollection children, bool self) { List itemsContainers= new List (); + if (self) + { + itemsContainers.Add(this); + } if (children != null) { foreach (var item in children) { if (item.Contains != null) { - itemsContainers.AddRange(item.Contains); foreach (var contain in item.Contains) { - itemsContainers.AddRange(contain.GetAllContain(contain.Children)); + itemsContainers.Add(contain); + itemsContainers.AddRange(contain.GetAllContain(contain.Children, false)); } } } diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs index 3be1688..4851f91 100644 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModel.cs @@ -3160,17 +3160,33 @@ namespace AIStudio.Wpf.DiagramDesigner { if (port.GetBounds().IntersectsWith(blockDesignerItemViewModel.GetBounds())) //如果两个位置相交 { - - port.DataItem.ShowConnectors = true; - if (port.CanAttachTo(blockDesignerItemViewModel) == true) + var innerport = port.GetAllContain(port.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemViewModel.GetBounds())).OrderByDescending(p => p.ContainLevel).FirstOrDefault(); + if (innerport != null) { - port.BeAttachTo = true; + innerport.DataItem.ShowConnectors = true; + if (innerport.CanAttachTo(blockDesignerItemViewModel) == true) + { + innerport.BeAttachTo = true; + } + else + { + innerport.DisableAttachTo = true; + } + return innerport; } else { - port.DisableAttachTo = true; + port.DataItem.ShowConnectors = true; + if (port.CanAttachTo(blockDesignerItemViewModel) == true) + { + port.BeAttachTo = true; + } + else + { + port.DisableAttachTo = true; + } + return port; } - return port; } } @@ -3190,7 +3206,7 @@ namespace AIStudio.Wpf.DiagramDesigner public void ClearNearContain() { - Items.OfType().ToList().SelectMany(n => n.Contains).Where(p => p.BeAttachTo == true || p.DisableAttachTo == true).ToList() + Items.OfType().ToList().SelectMany(n => n.GetAllContain()).Where(p => p.BeAttachTo == true || p.DisableAttachTo == true).ToList() .ForEach(p => { p.DisableAttachTo = false; p.BeAttachTo = false; diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/DefaultViewModel/BlockDesignerItemViewModel.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/DefaultViewModel/BlockDesignerItemViewModel.cs index 2abccef..6dbf62f 100644 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/DefaultViewModel/BlockDesignerItemViewModel.cs +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/DefaultViewModel/BlockDesignerItemViewModel.cs @@ -124,7 +124,7 @@ namespace AIStudio.Wpf.DiagramDesigner public virtual void AddChild(BlockDesignerItemViewModel child) { child.RemoveFromSelection(); - this.AddToSelection(true, false); + this.GetRootParent.AddToSelection(true, true); System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => { await Task.Delay(10); @@ -136,7 +136,8 @@ namespace AIStudio.Wpf.DiagramDesigner public virtual void RemoveChild(BlockDesignerItemViewModel child) { this.RemoveFromSelection(); - child.AddToSelection(true, false); + + child.AddToSelection(true, true); System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => { await Task.Delay(10); @@ -155,6 +156,26 @@ namespace AIStudio.Wpf.DiagramDesigner { return Contains?.FirstOrDefault(); } + } + + public List GetAllContain() + { + return Contains.SelectMany(p => p.GetAllContain(p.Children, true)).ToList(); + } + + public BlockDesignerItemViewModel GetRootParent + { + get + { + if (ParentContain == null) + { + return this; + } + else + { + return ParentContain.DataItem.GetRootParent; + } + } } } }