2 level Model not always setting all properties.

Oct 24, 2011 at 9:05 AM
Edited Oct 24, 2011 at 9:09 AM

I have following classes:

  • Organization class
    • Address property of type Address
  • Address class
    • CountryName property (string)
    • CountryCode property (string)
  • CountryInfo class
    • Name (string)
    • Code (string)

I have a ComboBox, with ItemsSource = ObservableCollection<CountryInfo>, SelectedItem bound to SelectedCountryInfo ViewModel registered property.

When the selected item in the combobox is changed, the SelectedCountryInfo of my ViewModel is correctly updated (with a CountryInfo with both Code and Name filled in e.g. "AF", "Afganistan"). So there is NO problem with the binding to the ComboBox.

If, I use a single [Model] of type Organization and in the Setter of SelectedCountryInfo in my ViewModel, I set the Organization.Address.CountryName and Organization.Address.CountryCode ViewModel, all goes well.

However, if I use 2 levels of [Model], one of type Organization and one of type Address that has a [ViewModelToModel("Organization"] and have CountryName and CountryCode registered properties that refer to the Address Model and I set the CountryName and CountryCode, only the Organization.Address.CountryName (through the [Model] indirection) is set (CountryCode field remains empty). This problem only happens the when the ComboBox SelectedItem changes the first time. If I change it a second time in the same run both fields are set. (Note that if I reverse the statements and first set the CountryCode and then the CountryName, it is the CountryCode that is correctly updated and the CountryName field then remains empty).

(For this case, I don't really need the 2 level model indirection, it was a leftover from a previous experiment, and without it my application works fine. But I wanted to report it anyhow).

Oct 26, 2011 at 3:01 PM

Can you please send me the demo project for this? Then I have the exact workings and I can easily see what is going wrong.

Oct 26, 2011 at 7:55 PM

Well, my project had too much dependancies (non-standard SL components, WCF service,...) to be able to hand it over as is. I created a new project that should show the problem... but that one works. Trying to figure out what is different between both.


Oct 26, 2011 at 8:00 PM

Found the difference.  The second project (the one without the depencancies) did not override the ValidateFields. Now I can simulate the problem in that one too. Will send that project by email.

Oct 26, 2011 at 8:40 PM

Have sent the zip with the project.

Oct 27, 2011 at 10:58 AM

Issue solved, will commit it soon.