static void AddAction(DiagramControl dc, IList newItems) { if (newItems == null || newItems.Count == 0) return; if (newItems.Count > 1) { if (dc.ItemsSource == null) return; if (dc.Check()) { dc.Bind(newItems); } } else //增加单一节点 { foreach (var newItem in newItems) { var model = newItem as DataModel; if (model == null) continue; var item = new DesignerItem(newItem, dc); item.ItemStyle = JsonConvert.DeserializeObject<ItemStyle>(dc.GetItemStyle(newItem)); item.ItemStyle.designerItem = item; var parentid = dc.GetPId(newItem); if (parentid.IsNotEmpty()) { var parent = dc.Manager.GetDesignerItemById(parentid); parent.ChildrenDesignerItems.Add(item); item.ParentDesignerItem = parent; dc.Manager.DrawChild(parent, item, new List<DesignerItem>()); if (dc.IsAddAfter)/*增加子节点*/ { var childs = dc.Manager.GetAllSubItems(parent).Where(x => !x.Equals(item)); var lastChild = !childs.Any() ? parent : childs.Aggregate((a, b) => Canvas.GetTop(a) > Canvas.GetTop(b) ? a : b); var below = dc.DesignerItems.Where(x => Canvas.GetTop(x) > Canvas.GetTop(lastChild)).ToList(); dc.AddToMessage("增加子节点", dc.Manager.GetTime(() => { below.ForEach(x => { Canvas.SetTop(x, Canvas.GetTop(x) + item.ActualHeight); }); Canvas.SetTop(item, Canvas.GetTop(lastChild) + lastChild.ActualHeight); })); dc.Manager.SetSelectItem(item); } else/*增加相邻节点*/ { var s = dc.DesignerCanvas.SelectionService.CurrentSelection.ConvertAll<DesignerItem>(x => x as DesignerItem); if (!s.Any()) return; var selectedItem = s.FirstOrDefault(); if (selectedItem == null) return; DesignerItem libling; var down = dc.DesignerItems.Where(x => x.Level == selectedItem.Level && Canvas.GetTop(x) > Canvas.GetTop(selectedItem) && x.ParentDesignerItem.Equals(selectedItem.ParentDesignerItem)).ToList(); if (down.Any()) { libling = down.Aggregate((a, b) => Canvas.GetTop(a) < Canvas.GetTop(b) ? a : b); var below = dc.DesignerItems.Where(x => Canvas.GetTop(x) > Canvas.GetTop(libling)).ToList(); below.Add(libling); dc.AddToMessage("增加相邻节点", dc.Manager.GetTime(() => { below.ForEach(x => { Canvas.SetTop(x, Canvas.GetTop(x) + item.ActualHeight); }); Canvas.SetTop(item, Canvas.GetTop(libling) - libling.ActualHeight); })); } else { var childs = dc.Manager.GetAllSubItems(parent); var lastChild = !childs.Any() ? parent : childs.Aggregate((a, b) => Canvas.GetTop(a) > Canvas.GetTop(b) ? a : b); var below = dc.DesignerItems.Where(x => Canvas.GetTop(x) > Canvas.GetTop(lastChild)).ToList(); dc.AddToMessage("增加相邻节点", dc.Manager.GetTime(() => { below.ForEach(x => { Canvas.SetTop(x, Canvas.GetTop(x) + item.ActualHeight); }); Canvas.SetTop(item, Canvas.GetTop(lastChild) + lastChild.ActualHeight); })); } } } else { dc.Manager.DrawRoot(item); item.ApplyTemplate(); item.SetTemplate(); item.UpdateLayout(); if (dc.ItemsSource.Count > 1) { Canvas.SetTop(item, dc.GetTop(newItem) - item.ActualHeight / 2); Canvas.SetLeft(item, dc.GetLeft(newItem) - item.ActualWidth / 2); } else { Canvas.SetTop(item, 0); Canvas.SetLeft(item, 0); } } dc.DesignerItems.Add(item); dc.Manager.SetSelectItem(item); dc.Manager.Scroll(item); dc.Manager.SavePosition(); } } }
private static void DeleteAction(DiagramControl dc, IList oldItems) { if (oldItems == null || oldItems.Count == 0) return; dc.EnsureDeletedItems(); #region 先取得所有选中节点的子节点,再判断被删除的节点是否在内,是则不删除 var items = dc.Manager.GetSelectedItemsAndAllSubItems(); List<DesignerItem> allSubItems = items.Where(designerItem => !items.Contains(designerItem)).ToList(); #endregion foreach (var oldItem in oldItems) { var id = dc.GetId(oldItem); if (allSubItems.Any(x => x.ItemId == id)) { continue; } var deleteItem = dc.Manager.GetDesignerItemById(dc.GetId(oldItem)); if (deleteItem == null) continue; dc.Manager.DeleteArrange(deleteItem); var model = oldItem as DataModel; if (model == null) continue; //删除所有子节点 var children = dc.GetChildren(oldItem); children.ForEach(x => { var a = x as DataModel; if (a != null) { a.MarkDeleted(); dc.DeletedItems.Add(a); dc.Manager.DeleteItem(dc.GetId(a)); } }); //删除本身 model.MarkDeleted(); dc.DeletedItems.Add(oldItem); dc.Manager.DeleteItem(dc.GetId(oldItem)); //更新折叠按钮显示状态,选中节点,滚动到节点 var pid = dc.GetPId(oldItem); if (pid.IsNotEmpty()) { var parentDesignerItem = dc.Manager.GetDesignerItemById(pid); var subitems = parentDesignerItem.ChildrenDesignerItems; // dc.DiagramManager.GetDirectSubItemsAndUpdateExpander(parentDesignerItem);//更新展开按钮显示状态 if (subitems != null) { #region 选中上方相邻节点 var topItems = subitems.Where(x => Canvas.GetTop(x) < Canvas.GetTop(deleteItem)); var item = topItems.Any() ? topItems.Aggregate((a, b) => Canvas.GetTop(a) > Canvas.GetTop(b) ? a : b) : parentDesignerItem; dc.Manager.SetSelectItem(item); dc.Manager.Scroll(item); #endregion } } } //dc.DiagramManager.Arrange(); dc.Manager.SavePosition(); }