I figured it out now. It is important that the BuildOutputAst is able to return a null node, in case the SelectedIndex is -1. Also had to make constructor public (Can’t understand how I missed it earlier ). This is the code that works:
public class DummyDropdown : DSDropDownBase
{
public DummyDropdown() : base("Name of the output") { }
protected override SelectionState PopulateItemsCore(string currentSelection)
{
Items.Clear();
Dictionary<string, int> dropdownItems = new Dictionary<string, int> {
{ "Test1", 1 },
{ "Test2", 2 },
{ "Test3", 3 },
{ "Test4", 4 },
};
foreach (KeyValuePair<string, int> item in dropdownItems)
{
Items.Add(new DynamoDropDownItem(item.Key, item.Value));
}
return SelectionState.Restore;
}
public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes )
{
if (Items.Count == 0 ||
SelectedIndex == -1) //NB! This line is crucial for some reason
{
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 };
}
}