Skip to main content

Microsoft Silverlight

Answered Question Is this a ValueConverter scenario?RSS Feed

(0)

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

Is this a ValueConverter scenario?

I have a Person class as below. The person's CountryID is bound to a ComboBox. The data is stored internally as IDs that need to be looked up in the Codes class to show the human readable text in the ComboBox (France, Japan etc rather than 1,2). Is this a good scenario to use a ValueConvertor and any pointers on how to structure it as there will be multiple entities to lookup in different scenarios (Country,Occupation etc)?

public class Person
{
public int CountryID { get; set; }
public int OccupationID { get; set; }
}

public class Codes
{
public int Code { get; set; }
public string Decode { get; set; }
}

new Code( 1, "France" )
new Code( 2, "Japan" ) 

 

IanBlackburn
IanBlack...

Contributor

Contributor

2333 points

371 Posts

Re: Is this a ValueConverter scenario?

Possible, though I think I would prefer to provide the correct data in the first place.  You could use linq to build a new object with the data you need in it.  For example:

 List<Person> people = new List<Person>();
 List<Codes> codes = new List<Codes>();
 //TODO: populate people and codes with data...

           var results = from p in people
                          join c in codes on p.CountryID equals c.Code
                          select new { p.CountryID, c.Code };
            ListBox1.ItemsSource = results;

(If this has answered your question, "Mark as Answer" - many thanks)

Cheers

Ian Blackburn
SilverlightForBusiness.net

manish.dalal
manish.d...

Member

Member

99 points

21 Posts

Re: Is this a ValueConverter scenario?

You need to add one more property to Person class and expose Code as value of that property. Once that is done, you can databind SelectedItem property of ComboBox to the newly added property and DisplayMemberPath to the Description of Code. You can find details in my blog post on the same topic

http://feeds.feedburner.com/~r/ManishDalal/~3/417137421/combobox-in-datagrid.aspx

 

Manish Dalal
http://feeds2.feedburner.com/manishdalal

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

Re: Re: Is this a ValueConverter scenario?

Thanks Manish, excellent series of articles. I went down the route of adding SelectedValue to my controls rather than modify my data access classes. For lists, I use SelectedValue and for single item binding like textboxes, I use a custom ValueConverter.

Ian, when used across the whole app, I think it might clutter up the code a bit with custom joins for each binding. I'm also not sure how the CLR does mem management for strings; if I have one string object (e.g. 'France') in 100 objects, are there 100 instances in memory of the string?

IanBlackburn
IanBlack...

Contributor

Contributor

2333 points

371 Posts

Answered Question

Re: Re: Is this a ValueConverter scenario?

Using the linq expression to create the datasource would indeed require more memory, but the binding would be quicker than using the ValueConvertor because the computation is done in advance.  So I guess it depends on where you need to optimise, and what you feel is more maintainable.

 

(If this has answered your question, "Mark as Answer" - many thanks)

Cheers

Ian Blackburn
SilverlightForBusiness.net
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities