Dynamically populating a ContextualMenuManipulator

Hey all,

Fairly new to using certain aspects of UIElements and had a question. I have a block of code, which on init creates a manipulator for a contextual menu

private IManipulator CreateMoveSubgroupContextualMenu()
        {
            ContextualMenuManipulator contextualMenuManipulator = new ContextualMenuManipulator(
                menuEvent =>
               
                menuEvent.menu.AppendAction("Move Selection To Subgraph/Root", actionEvent => MoveCurrentSelectionToSubgraph(0))
                   
            );
            moveSubgraphMenu = contextualMenuManipulator;
            return contextualMenuManipulator;
        }

Now this list needs to be dynamically updated based on the current amount of subgraphs, so whenever a subgraph is added I am looking to call AppendAction to the DropdownMenu. I’m basically just looking to call the same AppendAction method as called in the constructor, but I cannot for the life of me figure out how to do this after storing the ContextualMenuManipulator. Any insight/help greatly appreciated

The callback passed to ContextualMenuManipulator is executed everytime a menu is shown; a new menu is created every time. This means you can add menu items dynamically in this callback. For example:

        private IManipulator CreateMoveSubgroupContextualMenu()
        {
            return new ContextualMenuManipulator(menuEvent =>
                {
                    for (int i = 0; i < subGraphCount; i++)
                    {
                        menuEvent.menu.AppendAction($"Move Selection To Subgraph/Root {i}", actionEvent => MoveCurrentSelectionToSubgraph(i));
                    }
                }
            );
        }

In this example, subGraphCount could be a property that calculates the current number of subgraphs, that way, everytime the menu is shown, it would generate menu entries for each existing subgraph.

1 Like