Silverligh Combobox databound to CSLA object

Silverligh Combobox databound to CSLA object

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


decius posted on Wednesday, August 05, 2009

I was just curious what other people are doing for this typical "everyday" sort of design issue.

When binding my CSLA list object to a Silverlight ComboBox, I need a default value in the combobox, like the typical "--Select an item--" of webforms. 

Problem is, if databinding, the only way to resolve this I see is to either include this value in the BO data access (which couples the business logic the the UI ...  ) or handle the data changed event, and modify the Csla list object right there (to which you end up having to create public ctors for the info class!!! yuk!)....

Either way seems a little messy to me, is this just a situation where you have to "break some eggs to make an omlet" as the old saying goes? Or is there some tricks of the trade I'm missing.

I'm a newb to Silverlight, so maybe i'm just missing something....

RockfordLhotka replied on Wednesday, August 05, 2009

One answer is on this FAQ page

http://www.lhotka.net/cslanet/faq/SilverlightFaq.ashx

decius replied on Thursday, August 06, 2009

Thank you, this SelectedValue concept is certainly what I am used to from Winforms and Webforms and solves a different problem I had regarding binding.

However, I'm still curious about how to get a default item in the combobox like "-All-". I'm just curious how other people handle that. Perhaps this is more of a basic Silverlight question. I will try the Silverlight forums.

decius replied on Thursday, August 06, 2009

Indeed, the Silverlight forums gave me some wisdom almost immediately.

http://silverlight.net/forums/p/70393/169553.aspx#169553

Sometimes the simplest solution is the best.  I would not have thought of that, and it's so easy!

The only thing I don't like about it is that I end up with a bunch of handled SelectionChanged events just to update the UI:

private void ddl_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

if (e.AddedItems[0] != null)

txt.Visibility = Visibility.Collapsed;

}

RockfordLhotka replied on Thursday, August 06, 2009

Having a default item in a combobox is another one of those permanent problems that exists in all UI technologies. I don’t have an answer, but I rather suspect the answer is the same in Silverlight as it is in Windows Forms, WPF and Web Forms…

 

The two broad solutions I’ve seen are to:

 

Create a custom control that shows the default item but doesn’t set that item into the underlying object (because it isn’t a valid item).

 

Or

 

Add the default item to the NVL business object, because the default item is a valid item with business meaning, and it can be selected. The ProjectTracker sample app shows how to implement a NVL that provides a default value – and any business object using that NVL gets that default value and uses it to set the appropriate business object property to that default value.

 

Copyright (c) Marimer LLC