Create Simple DropDown Node

Hey,

i am trying to create a custom dropDown-Node and used the given template from: https://github.com/DynamoDS/DynamoSamples/blob/master/src/SampleLibraryUI/Examples/DropDown.cs

In order to get all needed references for my library i installed the DynamoVisualProgramming.WpfUILibrary Nuget-package. So everything looks fine and there are no errors or warning.

After importing my library.dll i got two new nodes and i expected to see a drop-down list with following items:

new DynamoDropDownItem("Tywin", 0),
new DynamoDropDownItem("Cersei", 1),
new DynamoDropDownItem("Hodor",2)

My result is:

What i am missing?

template.cs:

using System.Collections.Generic;
using CoreNodeModels;
using Dynamo.Graph.Nodes;
using Dynamo.Utilities;
using ProtoCore.AST.AssociativeAST;
using Newtonsoft.Json;

namespace SampleLibraryUI.Examples
{
    [NodeName("Drop Down Example")]
    [NodeDescription("An example drop down node.")]
    [IsDesignScriptCompatible]
    public class DropDownExample : DSDropDownBase
    {
        public DropDownExample() : base("item"){}

        // Starting with Dynamo v2.0 you must add Json constructors for all nodeModel
        // dervived nodes to support the move from an Xml to Json file format.  Failing to
        // do so will result in incorrect ports being generated upon serialization/deserialization.
        // This constructor is called when opening a Json graph. We must also pass the deserialized 
        // ports with the json constructor and then call the base class passing the ports as parameters.
        [JsonConstructor]
        public DropDownExample(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base("item", inPorts, outPorts) { }

        protected override SelectionState PopulateItemsCore(string currentSelection)
        {
            // The Items collection contains the elements
            // that appear in the list. For this example, we
            // clear the list before adding new items, but you
            // can also use the PopulateItems method to add items
            // to the list.

            Items.Clear();

            // Create a number of DynamoDropDownItem objects 
            // to store the items that we want to appear in our list.

            var newItems = new List<DynamoDropDownItem>()
            {
                new DynamoDropDownItem("Tywin", 0),
                new DynamoDropDownItem("Cersei", 1),
                new DynamoDropDownItem("Hodor",2)
            };

            Items.AddRange(newItems);

            // Set the selected index to something other
            // than -1, the default, so that your list
            // has a pre-selection.

            SelectedIndex = 0;
            return SelectionState.Restore;
        }

        public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
        {
            // Build an AST node for the type of object contained in your Items collection.

            var intNode = AstFactory.BuildIntNode((int)Items[SelectedIndex].Item);
            var assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), intNode);

            return new List<AssociativeNode> {assign};
        }
    }
}

hi @MasyMo have you tried any other sample?

This one is without JsonConstructor, I believe its working.

Hey @jean,

thanks for your awnser. Sadly it still doesnt work.
grafik

Update:

I solved the problem by importing the library through a package. I my case it was necessary to create the package as an local one with the Dynamo prompt. As long as i only copied the files in my package folder dynamo installed it but never imported the library properly.

Here is the post: C# Dropdown

grafik

using System.Collections.Generic;
using CoreNodeModels;
using Dynamo.Graph.Nodes;
using Dynamo.Utilities;
using ProtoCore.AST.AssociativeAST;
using System.Windows;

namespace SampleLibraryUI.Examples { 

        [NodeName("Drop Down Example")]
        [NodeDescription("An example drop down node.")]
        [IsDesignScriptCompatible]
        public class DropDownExample : DSDropDownBase
        {
            public DropDownExample() : base("item") { }

            protected override SelectionState PopulateItemsCore(string currentSelection)
            {
                // The Items collection contains the elements
                // that appear in the list. For this example, we
                // clear the list before adding new items, but you
                // can also use the PopulateItems method to add items
                // to the list.

                Items.Clear();

                // Create a number of DynamoDropDownItem objects 
                // to store the items that we want to appear in our list.

                var newItems = new List<DynamoDropDownItem>()
            {
                new DynamoDropDownItem("Tywin", 0),
                new DynamoDropDownItem("Cersei", 1),
                new DynamoDropDownItem("Hodor",2)
            };

                Items.AddRange(newItems);

            // Set the selected index to something other
            // than -1, the default, so that your list
            // has a pre-selection.
                
                SelectedIndex = 0;
                return SelectionState.Restore;
            }

            public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
            {
                // Build an AST node for the type of object contained in your Items collection.

                
                if (Items.Count == 0 ||
                  SelectedIndex == -1) //NB! This line is crucial for some reason
                {
                    MessageBox.Show("Item.Count gleich 0");
                    return new[] { AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), AstFactory.BuildNullNode()) };
                }

                var intNode = AstFactory.BuildIntNode((int)Items[SelectedIndex].Item);
                var assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), intNode);

                return new List<AssociativeNode> { assign };
            }
        }
    }
1 Like