Binding a property, containing a child object, to a combo box

Binding a property, containing a child object, to a combo box

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


xxxJasonxxx posted on Friday, September 21, 2007

Hi guys,

 

Thanks for taking the time to read my post.

 

I’ve run into a weird programming challenge that occurs when I need to bind a property, that gets/sets a child object, to a combo box.  This situation occurs in multiple places throughout my business objects and my UI.  I suspect many of you guys have also run into this and I’m hoping you can offer some suggestions.

 

Here's an example - I have an editable “Payment” object with a child “Type” (payment type) object…

 

PAYMENT, editable parent object:

     - Id  (read only property, integer)

     - Amount  (editable property, decimal)

     - Type  (editable property, business object)

     - other editable properties

 

TYPE, read only child object:

     - Id  (read only property, integer)

     - Description  (read only property, string)

     - other read only properties

 

So it is totally clear.....the "Type" or Payment Type I'm talking about would be things like cash, Mastercard, Visa, check, etc.

 

I am binding all of the Payment object’s properties to Windows form controls to eliminate as much UI coding as possible.  It’s worked fine for properties that are strings or numbers, like “Id” or “Amount” – I bound them to a textbox and I was done (no UI code!).  But then I looked at binding the “Type” property (object) to a combo box and things got ugly.

 

I am populating the “Payment Type” combo box using a separate CSLA Name-Value-List object that contains the Ids and Descriptions for the various payment types.  I bound the combo box’s “SelectedValue” property to Payment.Type.Id – this binding made it so that when the object is changed, the combo box automatically changes itself to whatever payment type is selected in the object.  BUT, the change relationship does not work in reverse – a change to the combo box does not update the object.  That doesn’t work because the combo box selects an integer (type id) and the payment object’s “Type” property gets/sets a “Type” (payment type) object.  So I am forced to write one line of code (oh no!) in the combo box’s “LostFocus” or “SelectedIndexChanged” handler to instantiate and assign a new object to the “Type” property.

 

I’m trying hard to make it so our UI developers write as little code as possible.  So I really want to get rid of that one line of code!  I’ve explored various solutions to eliminate this line of code, all of which I do NOT think are very clean or elegant.  The best of the bad solutions I’ve come up with is this…

 

I’m considering adding a new editable property to the Payment object called “TypeId” (integer).  Then I’d change the original “Type” property (object) to be read only.  I still want to keep the original “Type” property (object) because its data is used for display purposes other UI screens.  My thought is that the new editable “TypeId” property (integer) will not only change itself but, behind the scenes, it will also change the original “Type” property (object) too.  Then I’d bind to combo box to the new “TypeId” property (integer) and changes would then be affected in both directions – object to combo box and combo box to object. 

However…

 

The nit-picky, anal-retentive side of me does not like the fact that there is going to be somewhat duplicate properties…..Type and TypeId.

 

Has anyone else run into this and thought of a more elegant solution?

 

Thanks,

 

Jason

xxxJasonxxx replied on Wednesday, September 26, 2007

Or has anyone run into this same problem and resolved it the same way I did?

Or have I done something weird with my object design and created a situation that should not ever occur?  (the unusual lack of responses makes me concerned about this)

Jason

ajj3085 replied on Wednesday, September 26, 2007

Hi

I've done this, and hit my head on it many times.  I solved it by creating an NVL of Types, and then having Payment's Type be a simple string.  In the property getter or setter, I look use GetKey of NVL to get the id of the payment type, and store that.

This should work if you have a more complex BO as a Value part of the NVL as well.. but you'll probably need to code a method that looks for the type's name and returns the correct key.

HTH
Andy

Copyright (c) Marimer LLC