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!

18 comments:

Tech IT Solutions 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.

custom writing service reviews 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

Robert B said...

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

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

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.

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

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

ahmed5987 said...

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

Pinoy Swertres said...

Enjoy your famous family web blog please bookmarks pinoy Channel to watch abs-cbn and gma shows.

Bunny .cow said...

ปล่อยใจสนุกสุดมันส์ไปกับ Sagaming ผู้ให้บริการบาคาร่าออนไลน์สุดทันสมัยและครบวงจร บริการครบจบในเว็บเดียว มีรูปแบบการเล่นที่หลากหลาย และห้องเดิมพันสุดพิเศษ VIP ให้เลือกเล่นมากกว่า 20 ห้อง เล่นง่าย ได้เงินจริง และจ่ายเงินไวใน 1 นาที และมีโปรโมชั่นต้อนรับสมาชิกใหม่ และโปรโมชั่นเอาใจสมาชิกเก่าอีกมากมาย แจกหนักกจัดเต็มไม่ยั้ง และจัดกิจกรรมดี ๆ ให้ผู้เล่นได้ร่วมสนุกลุ้นรับของรางวัลอื่น ๆ อีกเพียบ อัพเดตกิจกรรมทุกเดือน และมีโปรโมชั่นบาคาร่าสุดคุ้มแจกฟรีตลอดทั้งปี และยังมีระบบฝากถอนที่ปลอดภัยอีกด้วย ฝากง่าย ถอนเงินไว ด้วยแอพพลิเคชั่น True Money Wallet ฟรี ไม่มีค่าบริการ และสามารถทำรายการด้วยตนเองง่าย ๆ ไม่ต้องผ่านแอดมินอีกต่อไป

Angel17 said...

I find this post so informative. Thanks for sharing this one! real estate agent ma

Harry kevin said...

Your leadership skills are exceptional. Your guidance shapes success and inspires confidence, I am looking to hire assignment expert online for my academic assignment how to find best assignment help for me, If you have any knowledge let me know.

Greenwave Clean said...

For professional Window cleaning in mooresville nc, look no further. Our dedicated team is committed to providing top-notch service, ensuring your windows sparkle and shine. With years of experience, we pride ourselves on attention to detail, using industry-leading techniques and eco-friendly products for a streak-free finish. Whether it's residential or commercial, our skilled professionals cater to your specific needs.

We offer flexible scheduling, competitive rates, and a satisfaction guarantee. Enhance the curb appeal of your home or business with pristine, crystal-clear windows. Trust us for reliable, efficient, and friendly window cleaning services in Mooresville, NC. Your satisfaction is our priority.

Anonymous said...

Phenomenal article! Keep up the fantastic work. social media promotion agency gwalior

simrankaur said...

Probyto assures that insights are efficiently conveyed to important stakeholders by means of clear reports and visuals. It doesn't merely produce insights. The goal of Probyto's Business Intelligence services is to maximize insights for businesses through the creation of dynamic charts, interactive dashboards, and configurable reports.

brandmart said...

Thanks for sharing nice and valuable information, I was looking for it. Really appreciated. Also visit swiftmarthub.com/ for best Exploring Salesforce services in world