예제 #1
0
        /// <summary>
        /// Create a node and add it to the view-model.
        /// </summary>
        public NodeViewModel CreateNode(string name, Point nodeLocation, bool centerNode)
        {
            var node = new NodeViewModel(name, nodeLocation);

            node.AddInputConnector();
            node.AddInputConnector();
            node.AddOutputConnector();
            node.AddOutputConnector();

            if (centerNode)
            {
                //
                // We want to center the node.
                //
                // For this to happen we need to wait until the UI has determined the
                // size based on the node's data-template.
                //
                // So we define an anonymous method to handle the SizeChanged event for a node.
                //
                // Note: If you don't declare sizeChangedEventHandler before initializing it you will get
                //       an error when you try and unsubscribe the event from within the event handler.
                //
                EventHandler <EventArgs> sizeChangedEventHandler = null;
                sizeChangedEventHandler =
                    delegate(object sender, EventArgs e)
                {
                    //
                    // This event handler will be called after the size of the node has been determined.
                    // So we can now use the size of the node to modify its position.
                    //
                    node.X -= node.Size.Width / 2;
                    node.Y -= node.Size.Height / 2;

                    //
                    // Don't forget to unhook the event, after the initial centering of the node
                    // we don't need to be notified again of any size changes.
                    //
                    node.SizeChanged -= sizeChangedEventHandler;
                };

                //
                // Now we hook the SizeChanged event so the anonymous method is called later
                // when the size of the node has actually been determined.
                //
                node.SizeChanged += sizeChangedEventHandler;
            }

            //
            // Add the node to the view-model.
            //
            this.Network.Nodes.Add(node);

            return(node);
        }
예제 #2
0
        public static void AttachInputAndOutputConnectors(NodeViewModel node, int inputCount, int outputCount)
        {
            for (var i = 0; i < inputCount; i++)
            {
                node.AddInputConnector();
            }

            for (var i = 0; i < outputCount; i++)
            {
                node.AddOutputConnector();
            }
        }