Binding a property to 2 models

Oct 19, 2011 at 9:34 AM

Suppose I have following classes and properties, and I want to allow the user to enter the Address.CountyName by selecting it from a list in a ComboBox.

  • CountryInfo
    • Name (e.g. Belgium)
    • Code (e.g. BE)
  • Address
    • CountryName (e.g Belgium)

In my ViewModel a have

  • ObservableCollection<CountryInfo> (filled from database with valid countries) that I use as ItemsSource of the ComboBox
  • SelectedCountryInfo that is bound to the SelectedItem of the ComboBox
  • Address model property
  • CountryName property... It seems that I need link this to 2 models (the Address model and the SelectedCountryInfo model) Is this possible?


Oct 19, 2011 at 10:01 AM

This is very hard because the SelectedItem actually isn't a model, it's just a selected value where you want to use a value from. In this case, I would add a change handler to the property definition (just another overload of the RegisterProperty method), and set the CountryName in that handler.

If this is not clear, just let me know.