I’m in a curious time-warp this week, as I’ve just been asked how to do something in WF4 that I wrote about for WF3 in September 2005. Anyhow, a customer of mine asked how to add an item to the context menu for an activity within the design surface, and so here goes…

Firstly you need a custom designer. Once you have a designer, open up the code behind file and create a new property of type ICommand.

/// <summary>
/// Custom activity
/// </summary>
public partial class CustomSchedulerDesigner
{
    /// <summary>
    /// Command used to add a banch to my activity at design time
    /// </summary>
    public ICommand AddBranch { get; set; }

}

My activity has branches (in a similar concept to the Pick activity and the Parallel activity) so AddBranch seemed like a fairly reasonable name for the command. Some folk subscribe to the notion that all commands should be suffixed with ‘Command’, so if you would prefer please feel free to call yours AddBranchCommand. Me – I’m fine with the above.

Next you need an implementation of ICommand to actually do the work (and optionally enable/disable the command). What you choose is up to you – I use a DelegateCommand class that I’ve had hanging around in my snippets in VS for a year or two, you may have your own but if not a quick search on the web will bring up many DelegateCommand classes. So, I then need to add a delegate command that will be called when the user clicks my menu item. For my example the code is as simple as the following…

/// <summary>
/// Custom activity
/// </summary>
public partial class CustomSchedulerDesigner
{
    /// <summary>
    /// Command used to add a banch to my activity at design time
    /// </summary>
    public ICommand AddBranch { get; set; }

    public CustomSchedulerDesigner()
    {
        InitializeComponent();

        AddBranch = new DelegateCommand(x => 
this.ModelItem.Properties["Branches"]
.Collection.Add(new CustomSchedulerBranch())); } }

Here I’m using a Lambda expression for the delegate (I love ‘em) and it looks up the Branches property of my custom activity, and to it adds a new instance of the CustomSchedulerBranch activity. The ModelItem property of a custom designer isn’t strongly typed hence the somewhat weird syntax, but you could do some work to get the actual activity instance if you wished. I took the quick route!

With that little lot in place all you then need to do is wire up the command inside the designer. In my XAML I added the following…

<sap:ActivityDesigner.ContextMenu>
    <ContextMenu>
        <MenuItem Header="Add Branch" Command="{Binding AddBranch}"/>
    </ContextMenu>
</sap:ActivityDesigner.ContextMenu>

And then you can compile your code and you should see a new menu item on screen when you display the context menu over your activity…

AddBranch

Voila! Clicking on the “Add Branch” menu item calls your delegate and you can then do whatever you wish within the designer code. I’ve not provided an example as mine’s been hacked around a bit but hopefully there’s enough here to make it fairly easy to follow along. The jaunty tilt in the image above is a feature of Windows Live Writer, which I use to create my blog posts. I just noticed that I could tilt images so couldn’t resist.

Bye for now!

 

Originally posted by Morgan Skinner on 5th July 2010 here http://blogs.msdn.com/b/morgan/archive/2010/07/05/adding-a-context-menu-item-to-a-workflow-4-designer.aspx