Binding Property To Multi-Select List Box

Binding Property To Multi-Select List Box

Old forum URL: forums.lhotka.net/forums/t/6100.aspx


xxxJasonxxx posted on Wednesday, January 07, 2009

I'm binding one of my business objects to my UI. 

I've done a single-selection drop down list before by binding to an Integer property on my object.....no problem.

But now I've got a multi-select list box on the UI and I need to create a new property on the object to bind to.  What is the best way to create a property to handle this multiple selection?

 

 

ajj3085 replied on Thursday, January 08, 2009

Well, what's the use case?  Does the BO need to know what objects are selected?

xxxJasonxxx replied on Thursday, January 08, 2009

Yes, the business object needs to know which options are selected and which options are not selected.  Right now there are just 3 options, but that number could increase in the future.

I had 2 initial ideas, but I don't think they're a good solution:

1.) Try to use a string property and somehow store/bind the selections as a comma separated string.  I'm pretty sure this will NOT work.

2.) Create a property that contains a collection of "Option" objects.  The "Option" object would have 3 properties: Id (Integer), Description (String), Selected (Boolean).  I think I could get this to work but I have doubts about whether this is a good solution.

Multi-select list boxes are a commonly used control.....so surely someone else has run into this.  Can anybody make some suggestions or give examples of how you've handled this? 

 

 

 

ajj3085 replied on Thursday, January 08, 2009

Option 2 would sound like your best bet.  I assume you're going to store the fact that one of the options is selected, correct?

lukky replied on Thursday, January 08, 2009

Just an idea, but would a "flagged enum" be thinkable ?

Make the BO property of type YourEnum, and since it's a flagged enum, you can use a mask to check which one is active/inactive. Then it becomes a matter of making the UI control display those properly.

If you need to map some more data to each enum, like a description etc, then a dictionnary<MyEnum, MyEnumInfo> could be used, where MyEnumInfo would hold all thge necessary data associated with each option.

How does that sound ?

Regards

 

robert_m replied on Thursday, January 08, 2009

I implemented option 2 in one or two of my business classes and was quite happy with it

xxxJasonxxx replied on Thursday, January 08, 2009

I have the Id (Integer) and Description (String) binding correctly. 

But I'm struggling with the Selected (Boolean).

How did you bind the boolean "Selected" property on your busines object to the "Selected" property on the ListItem?

 

lukky replied on Thursday, January 08, 2009

Hi,

I don't know if this would help the Original Poster, but please take a look at the attached sample C# project.

It implements the idea I mentioned in my previous post in this thread.

Hope this helps.


Copyright (c) Marimer LLC