private void SetConnectorDecoratorTemplate(DesignerItem item) { if (item.ApplyTemplate() && item.Content is UIElement) { ControlTemplate template = DesignerItem.GetConnectorDecoratorTemplate(item.Content as UIElement); Control decorator = item.Template.FindName("PART_ConnectorDecorator", item) as Control; if (decorator != null && template != null) { decorator.Template = template; } } }
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 void SetConnectorDecoratorTemplate(DesignerItem item) { if (item.ApplyTemplate() && item.Content is UIElement) { ControlTemplate template = DesignerItem.GetConnectorDecoratorTemplate(item.Content as UIElement); Control decorator = item.Template.FindName("PART_ConnectorDecorator", item) as Control; if (decorator != null && template != null) decorator.Template = template; } }