locked
Adding items to MultipleItemsContextMenu RRS feed

  • Question

  • So far adding my own ContextMenu items for SINGLE selected items in the Workflow is working pretty well. In my rehosted application it looks like this:

    System.Windows.Controls.MenuItem myItem = new System.Windows.Controls.MenuItem
    {
           Header = "some header",
    };
    
    designer.ContextMenu.Items.Add(myItem);

    When enabling MultipleItemsContextMenuEnabled (WF 4.5 feature) and right clicking on an item while MULTIPLE items are selected, you get a context menu for the first time:

    I have not figured out how to add items to this context menu, these four items are always shown. Can anyone help?
    Thank you!



    • Edited by lyndon23 Thursday, April 23, 2015 5:51 AM
    Wednesday, April 22, 2015 1:25 PM

Answers

  • Hi,

    We can Intercept right-click event at the main window or workflow designer level and walk up the visual tree from the right-click recipient, looking for a StateMachine designer, or: walk down the model item tree looking for a tateMachine

    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Right)
            {
                DependencyObject node = e.OriginalSource as DependencyObject;
                while (node != null)
                {
                    if (node is ActivityDesigner)
                    {
                        break;
                    }
                    else
                    {
                        node = VisualTreeHelper.GetParent(node);
                    }
                }
                if (node.GetType().Name.Contains("StateMachineDesigner"))
    
                {
    
                    var designer = (ActivityDesigner)node;
    
                    if (designer.ContextMenu == null)
                    {
                        designer.ContextMenu = new ContextMenu();
    
                        MenuItem menuItem = new MenuItem
                        {
                           Header = "Create State",
                            Command = null,
                        };
                       designer.ContextMenu.Items.Add(menuItem);
                    }
                }
            }
            base.OnPreviewMouseDown(e);
        }

    Source from http://blogs.msdn.com/b/tilovell/archive/2011/06/24/wf4-enhancing-the-context-menu-for-built-in-activitydesigners-and-statemachine.aspx

    #similar discussion:

    https://social.msdn.microsoft.com/forums/vstudio/en-US/1ba1186a-d4f2-4e5c-a04b-63cb844c7680/context-menu-in-rehosted-workflow-designer


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 28, 2015 7:07 AM

All replies

  • Hi,

    We can Intercept right-click event at the main window or workflow designer level and walk up the visual tree from the right-click recipient, looking for a StateMachine designer, or: walk down the model item tree looking for a tateMachine

    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Right)
            {
                DependencyObject node = e.OriginalSource as DependencyObject;
                while (node != null)
                {
                    if (node is ActivityDesigner)
                    {
                        break;
                    }
                    else
                    {
                        node = VisualTreeHelper.GetParent(node);
                    }
                }
                if (node.GetType().Name.Contains("StateMachineDesigner"))
    
                {
    
                    var designer = (ActivityDesigner)node;
    
                    if (designer.ContextMenu == null)
                    {
                        designer.ContextMenu = new ContextMenu();
    
                        MenuItem menuItem = new MenuItem
                        {
                           Header = "Create State",
                            Command = null,
                        };
                       designer.ContextMenu.Items.Add(menuItem);
                    }
                }
            }
            base.OnPreviewMouseDown(e);
        }

    Source from http://blogs.msdn.com/b/tilovell/archive/2011/06/24/wf4-enhancing-the-context-menu-for-built-in-activitydesigners-and-statemachine.aspx

    #similar discussion:

    https://social.msdn.microsoft.com/forums/vstudio/en-US/1ba1186a-d4f2-4e5c-a04b-63cb844c7680/context-menu-in-rehosted-workflow-designer


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 28, 2015 7:07 AM
  • Hi,

    I am using your approach already in my application, but so far only on single selected items. When the Multiple Items Selection feature is enabled

    DesignerConfigurationService configService =
                    wd.Context.Services.GetRequiredService<DesignerConfigurationService>();
                    configService.MultipleItemsContextMenuEnabled = true;

    ...I don't get access to the context menu i.e. I am not able to add items to it.

    However, my current workaround disables the option

    configService.MultipleItemsContextMenuEnabled = false;
    ..so I am able to set up an empty context menu. Then I am adding the standard commands like "Cut" and "Copy" next to my "own" menu items. Not very pretty, but works.
    Monday, May 4, 2015 6:08 AM