Create Simple DropDown Node

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