CSLA .NET

From Rockford Lhotka's Expert C# 2008 and VB 2008 Business Objects books

Binding Property To Multi-Select List Box

rated by 0 users
This post has 7 Replies | 2 Followers

Top 100 Contributor
Posts 66
xxxJasonxxx Posted: Wed, Jan 7 2009 4:38 PM

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?

 

 

Top 10 Contributor
Posts 3,715
Andy replied on Thu, Jan 8 2009 7:06 AM
Well, what's the use case?  Does the BO need to know what objects are selected?
Top 100 Contributor
Posts 66

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? 

 

 

 

Top 10 Contributor
Posts 3,715
Andy replied on Thu, Jan 8 2009 11:35 AM
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?
Top 50 Contributor
Posts 151
Luc Morin replied on Thu, Jan 8 2009 11:43 AM

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

 

Luc Morin, T.P.
http://www.stlm.ca

Top 200 Contributor
Posts 35
I implemented option 2 in one or two of my business classes and was quite happy with it
Top 100 Contributor
Posts 66

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?

 

Top 50 Contributor
Posts 151
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.


Luc Morin, T.P.
http://www.stlm.ca

Page 1 of 1 (8 items) | RSS

Please contact Magenic for your .NET consulting
and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems