Search code examples
workflow-foundation-4workflow-foundation

In custom WorkflowViewElement, how to support WF4.5 Annotations?


I've created a a custom activity designer, but I wanted to have control over the 'header' part (that by default shows the ModelItem.DisplayName), and as far I know, the only way to achieve this is to derive the designer from WorkflowViewElement rather than ActivityDesigner. So, that seems to work and all is well. Except, I want to support the new Annotations feature of WF4.5.
Given that when you add an annotation to a workflow element on the design surface it adds an icon to the element you are annotating + the actual annotation element, it seems clear that any custom WorkflowViewElement has to have some extra stuff in it to support this behaviour. Can anyone help?

Thanks


Solution

  • As I thought, annotations are saved within the workflow definition as an attached property. Here's what an annotation looks like in the workflow's xaml:

    <Sequence
      xmlns:derp="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
      derp:Annotation.AnnotationText="This is an annotation!">
    

    See, just like any other attached property. Except it isn't. Its an attached workflow property, not an attached DependencyProperty. That means it works through the attached property service as well as the Annotation class. Getting and setting the annotation text on a ModelItem is trivial (and covered below).

    Its actually not that hard to support annotations. As long as you don't mind your UI looking like crap. Here's a quick and dirty implementation.

    In the UI, add some controls for holding and editing the annotation text

    <sap:WorkflowViewElement
        x:Class="AnnotationSupport.MyActivityDesigner"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
        xmlns:ann="clr-namespace:System.Activities.Presentation.Annotations;assembly=System.Activities.Presentation"
        xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
        x:Name="root"
        MinWidth="100"
        MinHeight="100">
        <Grid Background="red">
            <Grid.RowDefinitions>
                <RowDefinition
                    Height="auto" />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Expander
                IsExpanded="False">          
                <!-- HERE SHE BLOWS -->
                <TextBox
                    Text="{Binding ModelItem.AnnotationText}" />
            </Expander>
                <TextBox
                Grid.Row="1"
                Text="{Binding ModelItem.Text}"
                Margin="10" />
        </Grid>
    </sap:WorkflowViewElement>
    

    Any time the Annotation text changes, the ModelItem's PropertyChanged event fires, just like any other property. And if you want to grab it from code, the simplest way is to cast the ModelItem to a dynamic:

    private void SetAnnotationLol(string newValue)
    {
        if(ModelItem != null)
            ((dynamic)ModelItem).AnnotationText = newValue;
    }
    

    Now, if you want to create a nice UI like the Fx Activities have... well...

    I'll leave it up to you to create a custom Adorner to handle display and editing of the annotation. Which actually isn't as hard as it first looks. If you haven't done one yet, here's your opportunity.