private void RestoreDiagramFromXElement(XElement root) { if (root == null) { return; } this.Children.Clear(); this.SelectionService.ClearSelection(); IEnumerable <XElement> itemsXML = root.Elements("DesignerItems").Elements("DesignerItem"); foreach (XElement itemXML in itemsXML) { Guid id = new Guid(itemXML.Element("ID").Value); DesignerItem item = DeserializeDesignerItem(itemXML, id, 0, 0); this.Children.Add(item); SetConnectorDecoratorTemplate(item); if ((item.Content is ContentControl conCtrl) && (conCtrl.Tag is ToolboxItemSettings toolboxItemSettings)) { item.NoDelete = toolboxItemSettings.NoDelete; item.Proportional = toolboxItemSettings.Proportional; var typePropertiesOwner = BuilderTypePropertiesOwner.CompileResultType(toolboxItemSettings.Properties, item.ID.ToString()); item.PropertiesHandler = Activator.CreateInstance(typePropertiesOwner); item.SetPropertiesValues(toolboxItemSettings.Properties); } } this.InvalidateVisual(); IEnumerable <XElement> connectionsXML = root.Elements("Connections").Elements("Connection"); foreach (XElement connectionXML in connectionsXML) { Guid sourceID = new Guid(connectionXML.Element("SourceID").Value); Guid sinkID = new Guid(connectionXML.Element("SinkID").Value); String sourceConnectorName = connectionXML.Element("SourceConnectorName").Value; String sinkConnectorName = connectionXML.Element("SinkConnectorName").Value; Connector sourceConnector = GetConnector(sourceID, sourceConnectorName); Connector sinkConnector = GetConnector(sinkID, sinkConnectorName); Connection connection = new Connection(sourceConnector, sinkConnector); Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element("zIndex").Value)); this.Children.Add(connection); } }
private Connector GetConnector(Guid itemID, String connectorName) { DesignerItem designerItem = (from item in Enumerable.OfType <DesignerItem>(this.Children) where item.ID == itemID select item).FirstOrDefault(); Control connectorDecorator = designerItem.Template.FindName("PART_ConnectorDecorator", designerItem) as Control; connectorDecorator.ApplyTemplate(); return(connectorDecorator.Template.FindName(connectorName, connectorDecorator) as Connector); }
internal DesignerItem GetSelectedDesignItem() { DesignerItem selectedItem = null; foreach (var selectable in CurrentSelection) { if (selectable is DesignerItem designerItem) { selectedItem = designerItem; } } return(selectedItem); }
private static DesignerItem DeserializeDesignerItem(XElement itemXML, Guid id, double OffsetX, double OffsetY) { DesignerItem item = new DesignerItem(id); item.Width = Double.Parse(itemXML.Element("Width").Value, CultureInfo.InvariantCulture); item.Height = Double.Parse(itemXML.Element("Height").Value, CultureInfo.InvariantCulture); item.ParentID = new Guid(itemXML.Element("ParentID").Value); item.IsGroup = Boolean.Parse(itemXML.Element("IsGroup").Value); Canvas.SetLeft(item, Double.Parse(itemXML.Element("Left").Value, CultureInfo.InvariantCulture) + OffsetX); Canvas.SetTop(item, Double.Parse(itemXML.Element("Top").Value, CultureInfo.InvariantCulture) + OffsetY); Canvas.SetZIndex(item, Int32.Parse(itemXML.Element("zIndex").Value)); Object content = XamlReader.Load(XmlReader.Create(new StringReader(itemXML.Element("Content").Value))); item.Content = content; return(item); }
private void CopyCurrentSelection() { IEnumerable <DesignerItem> selectedDesignerItems = Enumerable.OfType <DesignerItem>(this.SelectionService.CurrentSelection); List <Connection> selectedConnections = Enumerable.OfType <Connection>(this.SelectionService.CurrentSelection).ToList(); foreach (Connection connection in Enumerable.OfType <Connection>(this.Children)) { if (!selectedConnections.Contains(connection)) { DesignerItem sourceItem = (from item in selectedDesignerItems where item.ID == connection.Source.ParentDesignerItem.ID select item).FirstOrDefault(); DesignerItem sinkItem = (from item in selectedDesignerItems where item.ID == connection.Sink.ParentDesignerItem.ID select item).FirstOrDefault(); if (sourceItem != null && sinkItem != null && BelongToSameGroup(sourceItem, sinkItem)) { selectedConnections.Add(connection); } } } XElement designerItemsXML = SerializeDesignerItems(selectedDesignerItems); XElement connectionsXML = SerializeConnections(selectedConnections); XElement root = new XElement("Root"); root.Add(designerItemsXML); root.Add(connectionsXML); root.Add(new XAttribute("OffsetX", 10)); root.Add(new XAttribute("OffsetY", 10)); Clipboard.Clear(); Clipboard.SetData(DataFormats.Xaml, root); }
private void Paste_Executed(object sender, ExecutedRoutedEventArgs e) { XElement root = LoadSerializedDataFromClipBoard(); if (root == null) { return; } // create DesignerItems Dictionary <Guid, Guid> mappingOldToNewIDs = new Dictionary <Guid, Guid>(); List <ISelectable> newItems = new List <ISelectable>(); IEnumerable <XElement> itemsXML = root.Elements("DesignerItems").Elements("DesignerItem"); double offsetX = Double.Parse(root.Attribute("OffsetX").Value, CultureInfo.InvariantCulture); double offsetY = Double.Parse(root.Attribute("OffsetY").Value, CultureInfo.InvariantCulture); foreach (XElement itemXML in itemsXML) { Guid oldID = new Guid(itemXML.Element("ID").Value); Guid newID = Guid.NewGuid(); mappingOldToNewIDs.Add(oldID, newID); DesignerItem item = DeserializeDesignerItem(itemXML, newID, offsetX, offsetY); this.Children.Add(item); SetConnectorDecoratorTemplate(item); newItems.Add(item); } // update group hierarchy SelectionService.ClearSelection(); foreach (DesignerItem el in newItems) { if (el.ParentID != Guid.Empty) { el.ParentID = mappingOldToNewIDs[el.ParentID]; } } foreach (DesignerItem item in newItems) { if (item.ParentID == Guid.Empty) { SelectionService.AddToSelection(item); } } // create Connections IEnumerable <XElement> connectionsXML = root.Elements("Connections").Elements("Connection"); foreach (XElement connectionXML in connectionsXML) { Guid oldSourceID = new Guid(connectionXML.Element("SourceID").Value); Guid oldSinkID = new Guid(connectionXML.Element("SinkID").Value); if (mappingOldToNewIDs.ContainsKey(oldSourceID) && mappingOldToNewIDs.ContainsKey(oldSinkID)) { Guid newSourceID = mappingOldToNewIDs[oldSourceID]; Guid newSinkID = mappingOldToNewIDs[oldSinkID]; String sourceConnectorName = connectionXML.Element("SourceConnectorName").Value; String sinkConnectorName = connectionXML.Element("SinkConnectorName").Value; Connector sourceConnector = GetConnector(newSourceID, sourceConnectorName); Connector sinkConnector = GetConnector(newSinkID, sinkConnectorName); Connection connection = new Connection(sourceConnector, sinkConnector); Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element("zIndex").Value)); this.Children.Add(connection); SelectionService.AddToSelection(connection); } } DesignerCanvas.BringToFront.Execute(null, this); // update paste offset root.Attribute("OffsetX").Value = (offsetX + 10).ToString(); root.Attribute("OffsetY").Value = (offsetY + 10).ToString(); Clipboard.Clear(); Clipboard.SetData(DataFormats.Xaml, root); }
private ContentControl CreateNewToolboxItem(ToolboxSettings toolboxSettings, ToolboxItemSettings itemsSetting) { var newItem = new ContentControl { Tag = itemsSetting, ToolTip = itemsSetting.DisplayName }; var stackPanel = new StackPanel { Orientation = Orientation.Horizontal }; var newGrid = new Grid(); var newPath = new Path { Style = itemsSetting.PathStyle /*, ToolTip = itemsSetting.DisplayName*/ }; newGrid.Children.Add(newPath); if (toolboxSettings.ToolboxGridType == ToolboxGrid.Grid) { newItem.Content = newGrid; } else { newGrid.Width = 25; stackPanel.Children.Add(newGrid); stackPanel.Children.Add(new TextBlock { Text = itemsSetting.DisplayName, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(10, 0, 0, 0) }); newItem.Content = stackPanel; } if (itemsSetting.PathStyle_DragThumb != null) { var controlTemplate = new ControlTemplate(); var pathTemplate = new FrameworkElementFactory(typeof(Path)); pathTemplate.SetValue(Path.StyleProperty, itemsSetting.PathStyle_DragThumb); controlTemplate.VisualTree = pathTemplate; DesignerItem.SetDragThumbTemplate(newItem, controlTemplate); } if (itemsSetting.Container != null) { var bindingLeft = new Binding("ActualWidth"); bindingLeft.Source = newGrid; var bindingTop = new Binding("ActualHeight"); bindingTop.Source = newGrid; var multiBinding = new MultiBinding(); multiBinding.Bindings.Add(bindingLeft); multiBinding.Bindings.Add(bindingTop); multiBinding.Converter = new RelativeMarginToMarginConverter(itemsSetting.Container.RelativeMargin); var button = new Button { Content = new TextBlock { Text = Properties.Resources.ClickToOpen, TextWrapping = TextWrapping.Wrap, HorizontalAlignment = HorizontalAlignment.Center, Visibility = Visibility.Collapsed } }; button.SetBinding(TextBlock.MarginProperty, multiBinding); newGrid.Children.Add(button); } if (itemsSetting.ConnectorsSettings != null) { var controlTemplate = new ControlTemplate(); var relPanelTemplate = new FrameworkElementFactory(typeof(RelativePositionPanel)); relPanelTemplate.SetValue(RelativePositionPanel.MarginProperty, new Thickness(-4)); if (itemsSetting.ConnectorsSettings.Any()) { foreach (var connectorsSetting in itemsSetting.ConnectorsSettings) { var connectorTemplate = new FrameworkElementFactory(typeof(Connector)); connectorTemplate.SetValue(Connector.NameProperty, connectorsSetting.Name); connectorTemplate.SetValue(Connector.OrientationProperty, connectorsSetting.Orientation); connectorTemplate.SetValue(RelativePositionPanel.RelativePositionProperty, connectorsSetting.RelativePosition); connectorTemplate.SetValue(Connector.MaxInConnectionsProperty, connectorsSetting.MaxInConnections); connectorTemplate.SetValue(Connector.MaxOutConnectionsProperty, connectorsSetting.MaxOutConnections); relPanelTemplate.AppendChild(connectorTemplate); if (!string.IsNullOrEmpty(connectorsSetting.Caption)) { var newCaption = new TextBlock { Text = connectorsSetting.Caption, IsHitTestVisible = false, Tag = connectorsSetting }; if (connectorsSetting.Orientation == ConnectorOrientation.Left) { newCaption.HorizontalAlignment = HorizontalAlignment.Right; } if (connectorsSetting.Orientation == ConnectorOrientation.Top) { newCaption.VerticalAlignment = VerticalAlignment.Bottom; } var bindingLeft = new Binding("ActualWidth"); bindingLeft.Source = newGrid; var bindingTop = new Binding("ActualHeight"); bindingTop.Source = newGrid; var multiBinding = new MultiBinding(); multiBinding.Bindings.Add(bindingLeft); multiBinding.Bindings.Add(bindingTop); multiBinding.Converter = new SizeToMarginConverter(connectorsSetting.RelativePosition, connectorsSetting.Orientation); newCaption.SetBinding(TextBlock.MarginProperty, multiBinding); newGrid.Children.Add(newCaption); } } } controlTemplate.VisualTree = relPanelTemplate; DesignerItem.SetConnectorDecoratorTemplate(newItem, controlTemplate); } return(newItem); }