Workflow Foundation 4.0 - Type Converter

Ambily.raj
Posted by in WWF category on for Intermediate level | Points: 250 | Views : 11475 red flag

Workflow Foundation 4.0 is introduced a significant amount of change in the Activity Designer. In WF4.0, Activity Designers are defined using WPF. Property management and display is one of the important aspects of a custom Activity Designer.

Introduction

Workflow Foundation 4.0 is introduced a significant amount of change in the Activity Designer. In WF4.0, Activity Designers are defined using WPF. Property management and display is one of the important aspects of a custom Activity Designer. We can use the PropertyValueEditor for providing meaningful editor for the Property. Also, we can use the TypeConverters for providing the expected values of a property.

Wf4.0 is using the WPF Type Converters. In this article we will discuss about the WPF TypeConverter and how we can use the same in WF4.0 custom activity.

Type Converter

Type Converters are used to pre-populate the expected values of a Property. We can either allow the user to edit new values or can restrict the user to only select from the given values using Type Converter.

Let us look into a simple Type converter to populate the WorkingDay property. Our Type Converter is inherited from StringConverter and we override three methods.

As we override the GetStandardValuesExclusive() method and return true, this Type Converter restrict the user only to select values from the list. If it return false or not overridden, then it display the list and also allow the user to enter another value.

Type Converter

public class WorkingDayConverter : StringConverter

    {

        private static StandardValuesCollection svc;

 

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)

        {

            return true;

        }

 

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)

        {

            return true;

        }

 

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)

        {

            ArrayList values = new ArrayList();

            values.Add("Monday");

            values.Add("Tuesday");

            values.Add("Wednesday");

            values.Add("Thursday");

            values.Add("Friday");

 

            svc = new StandardValuesCollection(values);           

            return svc;

        }

    }

Our TestActivity looks like 

public class TestActivity:CodeActivity

    {

        private string day;      

        [TypeConverterAttribute(typeof(WorkingDayConverter))]

        [BrowsableAttribute(true)]       

        public string WorkingDay

        {

            get

            {

                return this.day;

            }

            set

            {

                this.day = value;

            }

        }

        protected override void Execute(CodeActivityContext context)

        {

            MessageBox.Show(WorkingDay);

        }

    }

 
Workflow Designer will look like



Extracting Workflow Data

In the next sample, we will how to extract another activity data. For our sample Type Converter, I am populating the names of all WriteLine activities exist in our workflow. As I am not overriding the GetStandardValuesExclusive method, we can enter the values against the Property too.

Type Converter

internal class WriteLineConverter : StringConverter

    {

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)

        {

            return true;

        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)

        {

            ModelService modelService = (ModelService)context.GetService(typeof(ModelService));

            IEnumerable<ModelItem> activityCollection = modelService.Find(modelService.Root, typeof(WriteLine));

 

            ArrayList writeLines = new ArrayList();

            foreach (ModelItem shv in activityCollection)

            {

                writeLines.Add((shv.GetCurrentValue() as WriteLine).DisplayName);

            }

            return new StandardValuesCollection(writeLines);

        }

    }

Property in Test Activity

private string wrLine;

        [TypeConverterAttribute(typeof(WriteLineConverter))]

        [BrowsableAttribute(true)]

        public string WriteLinenames

        {

            get

            {

                return this.wrLine;

            }

            set

            {

                this.wrLine = value;

            }

        }

 
Workflow Designer 


Conclusion

WF4.0 Activity designers are defined using WPF. WPF integration gives full support for customization of our custom designer. In this article we discussed about the Type Converters used as part of custom properties in Activity Designer.

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)