Пример #1
0
        private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            XElement root = LoadSerializedDataFromClipBoard();

            if (root == null)
            {
                return;
            }

            // create DiagramElements
            Dictionary <Guid, Guid> mappingOldToNewIDs = new Dictionary <Guid, Guid>();
            List <ISelectable>      newItems           = new List <ISelectable>();
            IEnumerable <XElement>  itemsXML           = root.Elements("DiagramElements").Elements("DiagramElement");

            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);
                DiagramElement item = DeserializeDiagramElement(itemXML, newID, offsetX, offsetY);
                this.Children.Add(item);
                SetConnectionNodeDecoratorTemplate(item);
                newItems.Add(item);
            }

            // update group hierarchy
            SelectionService.ClearSelection();
            foreach (DiagramElement el in newItems)
            {
                if (el.ParentID != Guid.Empty)
                {
                    el.ParentID = mappingOldToNewIDs[el.ParentID];
                }
            }


            foreach (DiagramElement 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 oldTargetID = new Guid(connectionXML.Element("TargetID").Value);

                if (mappingOldToNewIDs.ContainsKey(oldSourceID) && mappingOldToNewIDs.ContainsKey(oldTargetID))
                {
                    Guid newSourceID = mappingOldToNewIDs[oldSourceID];
                    Guid newTargetID = mappingOldToNewIDs[oldTargetID];

                    String sourceConnectionNodeName = connectionXML.Element("SourceConnectionNodeName").Value;
                    String targetConnectionNodeName = connectionXML.Element("TargetConnectionNodeName").Value;

                    ConnectionNode sourceConnectionNode = GetConnectionNode(newSourceID, sourceConnectionNodeName);
                    ConnectionNode targetConnectionNode = GetConnectionNode(newTargetID, targetConnectionNodeName);

                    Connection connection = new Connection(sourceConnectionNode, targetConnectionNode);
                    Canvas.SetZIndex(connection, Int32.Parse(connectionXML.Element("zIndex").Value));
                    this.Children.Add(connection);

                    SelectionService.AddToSelection(connection);
                }
            }


            // update paste offset
            root.Attribute("OffsetX").Value = (offsetX + 10).ToString();
            root.Attribute("OffsetY").Value = (offsetY + 10).ToString();
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Xaml, root);
        }