Tuesday, 30 September 2014

Fun with ExpressionTextBox

After a short hiatus doing other things (a lot of back-end services work) I’m back in the fray with Workflow.

Yesterday, a colleague asked me to help out with an activity he was building that works like a combination of an If and a ForEach. The basic premise is that we have a List<Something>, and want to iterate through that list until we find a match with a Predicate<Something>, in which case we then schedule a child activity.

So, I came up with the following…


Here we have a collection of Noodle’s (as an argument called Stuff), and we’re going to iterate through all elements in the collection until we find the first that matches the predicate (x => x.Name == “Sausages”). When found, we then execute the Body activity, with item as the variable holding the selected Noodle instance.

The activity code is fairly simple…

    public class Find<T> : NativeActivity, IActivityTemplateFactory
public InArgument<IEnumerable<T>> Values { get; set; }

public ActivityAction<T> Body { get; set; }

public InArgument<Predicate<T>> Match { get; set; }

protected override void Execute(NativeActivityContext context)
var match = this.Match.Get(context);

// Iterate through the collection and if we find a match, we're sorted...
foreach(var o in Values.Get(context))
if (match(o))
context.ScheduleAction<T>(this.Body, o);

protected override void CacheMetadata(NativeActivityMetadata metadata)

public Activity Create(DependencyObject target)

The activity contains a collection of type T, a body that is passed the selected value from the collection, and a predicate that matches against the items in the collection. I’ve also overridden CacheMetadata to setup all of the arguments correctly, and in addition implemented IActivityTemplateFactory as there’s some setup that needs to be done when the activity is created in order to get it to work correctly. Full details are in the code download.

In the Execute method I iterate through the elements in the collection and execute the Match function against each. For the first that matches I then schedule the body activity, passing through the selected element. This then terminates the loop (that’s what we wanted, a more standard implementation might iterate though all matching elements in the collection.

The Designer

The important thing about the code is the designer (and that’s why I wrote the post in the first place). It’s fairly rudimentary, but there is something that might trip you up which is why I wrote this post.

The designer XAML contains a couple of ExpressionTextBox’s as shown in the image below…


The first is bound to the Values property on the activity, the second to the Match property. The important thing to note is that in order to get these to work properly you *must* use the ExpressionType property of the ExpressionTextBox…

Expression="{Binding ModelItem.Values, Mode=TwoWay,
Converter={StaticResource argToExpressionConverter}}"

OwnerActivity="{Binding ModelItem, Mode=OneWay}"
ExpressionType="{Binding ListType}"/>

Now, the type of items in the bound List is based on the type of element you choose to fill the list – in my case I have Noodles. So, the ExpressionType for the list would be IEnumerable<Noodle>, and the datatype for the predicate would be Predicate<Noodle>.

If you don’t use the ExpressionType property of the ExpressionTextBox then the text box will essentially be one-way, it will bind to values you set on the property grid, but won’t allow you to push values the other way (even if those values match 1:1 with what you type in the property grid). So, ExpressionType is mandatory, and has to be the right type.

In order to get the right type, I have created two properties on the Designer, and these call down to the underlying activity to get the actual type necessary. As an example here’s the code for the ListType…

    public Type ListType
if (null == _listType)
var findActivity = this.ModelItem.GetCurrentValue();

var valuesProp = findActivity.GetType().GetProperty("Values");

var args = valuesProp.PropertyType.GenericTypeArguments;

_listType = args[0];

return _listType;

private Type _listType;

This code gets the fund activity which is exposed through the ModelItem (which is a proxy object over the activity you are editing). I then lookup the Values property and return the first generic argument – in my example this will be IEnumerable<Noodle>. I have a similar property for the match predicate.

The Code

If you want to have look at the full code please click to download it…


Unknown said...



Richard Majece said...

I had similar task to do at home after classes. Then I found these homework help websites and did it very fast. You can use this info for your homework, too)

studentsassignmenthelp said...

Take professional Homework Help Canada services from the academic experts of StudentsAssignmentHelp.com. Our writers are expert in various academic disciplines such as law, IT, management, nursing, mass communication, economics, English, history, and many others.

meldaresearchusa said...

The vast experience we have in offering nursing writing services has enabled scores of students to score high grades in their assignment since we are known for offering the purchase term paper.

jacklinemelda said...

Why we rank the best in offering professional research papers for sale online is the quality services that are offered to the thousands of clients that contract our organization for the provision of quality custom writing services.

meldaresearch said...

If you have been wondering who will “Affordable Research Paper Writing Service Online”“write my research paper” or “write my paper” choose us today and avoid poor grades.We reliably provide services at the stated price without hidden charges. Contact us and let us help you at any stage of your Affordable Essay Writing Services.

Puremelda said...

Our top-notch writers have access to peer-reviewed articles, Free Essay Writer Services and online databases that are essential in writing Custom Writing Services on any chosen topic.

Assignment Help said...

This is a great article, that I really enjoyed reading. Thanks for sharing
Science Assignment Help

Puremelda said...

In the quest for quality grades, students have realized the need for working with Nursing Paper Writing Service agencies that specialize in various types of essay writing. They are the best company that handles Best Proofreading Service and Affordable Editing Services.

ahmed5987 said...

حتى إذا كنت تستخدم غطاء مرتبة بشكل دينى ، فإن الفرش يجمع الغبار وعث الغبار وتربة الجسم. انزع الغطاء وأغسله بالماء الساخن. لتنظيف المرتبة ، قم بتفريغ الهواء جيدًا في الجزء العلوي والجانبين ولا تنس نوابض الصندوق إذا كان لديك. افحص الفراش بحثًا عن أي بقع واعالجها بمحلول منظف الطبق والماء المعتدلين. استخدم فرشاة ناعمة واعمل من الخارج من البقعة باتجاه المركز لمنع الانتشار. شطف المنطقة بقطعة قماش مغموسة في الماء العادي ثم اتركه حتى يجف تمامًا. لتسريع التجفيف ، استخدم مجفف شعر على مستوى منخفض ولكن تجنب الحرارة الزائدة.
شركة تنظيف
شركة تنسيق حدائق بالرياض
شركة تنظيف منازل بالخرج
شركة تنظيف فلل بالرياض

Ashley said...

Are you looking for Science Assignment help? justquestionanswer.com is providing Science Assignments company in the USA from the qualified expert. Our 3000+ Science experts are available 24x7 to help the USA students and bring forth the peerless Science end of the day without asking an amount that exceeds the budget.

Sam said...

According to reports of survey, it is confirmed now that three out of every four student seek online assignment help from professionals in Australia. It also indicate that students who avail assignment help for their assignment or homework from professionals improve their grade by 20% than those students, who do not take any help. If you are stuck with your research projects, essays, case studies, business reports or need Urgent Assignment Help, then myassignmenthelp is the place to be.

noor said...

مكافحة حشرات بالخبر
مكافحة حشرات بمكة مكافحة حشرات بمكة
مكافحة حشرات بالمدينة المنورة مكافحة حشرات بالمدينة المنورة
مكافحة حشرات بالدمام مكافحة حشرات بالدمام

meldaresearch said...

Online education writing services companies have come up with Online Education Assignment Help for education and teaching studies students in order for them to score straight A’s in their custom education research paper services.

Term Paper Writing Services said...

Political science research paper writing help has become very popular among students seeking Political Science Writing Services and political science essay writing services.