Thursday, 23 October 2014

ComboBox binding in Workflow activities

Today I was creating a simple activity with a couple of combo boxes, and came across a problem where the binding wasn’t working correctly (well, at all). I had an InArgument<string> property on my activity, and wanted to set this from a combo box on the design surface. I guess I haven’t done this before as it doesn’t work out of the box, and you need to write an IValueConverter to get it working. I’ve trodden this path before but this was a bit of a tricky beast.

After some wailing and gnashing I came up with the following base class converter…

public abstract class ComboBoxToModelItemConverter<T> : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
T retVal = default(T);

ModelItem mi = value as ModelItem;

if (null != mi)
{
var arg = mi.GetCurrentValue() as InArgument<T>;

if (null != arg)
{
var expression = arg.Expression;
var literal = expression as Literal<T>;

if (null != literal)
retVal = literal.Value;
}
}

return retVal;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return new InArgument<T>((T)value);
}
}





I then created a concrete class to bind to InArgument<string>…

public class ComboBoxStringToModelItemConverter : ComboBoxToModelItemConverter<string>
{
}



With that created I could then get the binding to work by adding the converter to the binding. I have a class that contains TemplateId and Name, I wish to show the Name on screen but store the TemplateId (and this is mapped to an InArgument<string> on the activity). The XAML is as follows…

<local:ComboBoxStringToModelItemConverter x:Key="comboConverter"/>


<ComboBox ItemsSource="{Binding Templates}" 
SelectedValue="{Binding ModelItem.TemplateId, Converter={StaticResource comboConverter}}"
SelectedValuePath
="TemplateId" DisplayMemberPath="Name" />



With that done it all works as expected. I’m surprised that a converter such as this doesn’t exist in the Workflow libraries – and also in the many, many years I’ve been playing with Workflow that I’ve not needed one. Anyhow, I hope this is of use to someone!

17 comments:

Leadership and Organisational Excellence said...

Oh so you finally created the Base Class convertor in order to do the binding task in combo box.You have intelligently found out the way out.

best essays online said...

Wow looks pretty complex as well as amazing for a amateur like me, great post! Hope to see more of your work soon, you seem like a very talented programmer.

Oliver Maurice said...

Visit this awesome blog for some information on how to write a great research paper. I think that you will find lots of interesting info there

AngelaHolt said...

Thanks for sharing the information. Keep the good work going. Root explorer Viper4android youtubevanced

Robert B said...

Nice sharing amazing one keep sharing more thanks for all lovely one avian vet long island

meldaresearch said...

Students should ensure that quality of essays offered match their prices. All our Nursing Research Paper Writing Service should ensure that customers’ private details are kept in privacy. This also applies when one need to seek for write my term paper online.

Assignment Help said...

Thank for sharing this information. We provide writing help services at Australi. Hire Now!
Taxation Law Assignment Help

KeiraDoltan said...

Nice blog, The future of the students depends on the grades they obtain in their academic studies. High grades for better job opportunities.
Assignment Provider
All Assignment Help

meldaresearch said...

Our Research Writing Services is normally written by professionals who have sufficient experience in writing academic papers in any discipline. Our customer service is available 24/7, which makes it easy for clients to place orders and receive their Help in Essay Writing on time.

William Shell said...

Hey, Your blog is very informative. It is nice to read such high-quality content. Attractive information on your blog, thank you for taking the time and share with us.Myassignmenthelp delivers high-quality content related to science assignment help writing services.

Puremelda said...

You have all the reasons not to trust cheap paper writing services, Custom Writing Writers, and dissertation writing help. We offer the Write an Essay for Me to the clients at an affordable price.

Ethan Lee said...

We all are aware of the fact that it is too tough for anyone to write their, assignment, essay, thesis, dissertation or paper, also, there are lots of students who in search of good marks in their academics, search GOOGLE for Perdisco, Assignment Help, Essay Help, Thesis Help, Paper Help, Research Paper Help or Dissertation Editor, How to Write a Bibliography. But the problem is that these services are not pocket-friendly for every student.
So, What is the Solution?
You can use myassignmenthelp.com Paraphrasing Generator Tool, with the help of this tool, rewriting an article, essay or assignment is a work of just a few clicks, Also, we know the importance of Plagiarism Free work, that's why we developed our online paraphrasing tool in such a way that their will be no chance of plagiarism.

Unknown said...

I am very happy to visit your blog and read this article. This is the kind of information that needs to be given and not the random misinformation that’s at the other blogs. I appreciate your post. write my paper

Unknown said...

It’s good to know about your blog and its post. You have done pretty impressive work on this blog and it posts, Keep up doing well. affordable papers

Robert B said...

I read your blog post and this is nice blog post.. thanks for taking the time to share with us. have a nice day Network

norhan said...


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

Florahmelda said...

Our Professional Medicine Essay Writers work tirelessly to ensure that your Medicine Research Paper Writing is completed within the time frame given to avoid poor scores in your Medical Essay Assignments Writing.