Reputation: 979
I relatively new to mvvm, however I have a question about binding from a viewmodel. I have a viewmodel where in its constructor I want to populate a observablecollection. My constructor looks like this:
public StudySelectionViewModel() : base() {
_studyRepository = StudyRepository.Instance;
InitializeViewModelData();
}
The InitializeViewModelData() looks like this:
private void InitializeViewModelData() {
_studyRepository.RetrieveModalityTypes();
RaisePropertyChanged("ModalityTypes");
}
My property ModalityTypes looks like this:
public ObservableCollection<ModalityType> ModalityTypes {
get {
return _studyRepository.ModalityTypes;
}
}
Probably needless to say that my repository makes a call to a database and retrieves the data. When debugging it looks like that the RaisePropertyChanged is executed earlier than my _studyRepository.RetrieveModalityTypes method is executed and hence it binds to an empty property.
Am I missing something here? Is my design bad? Any ideas?
Thanks in advance,
Upvotes: 1
Views: 99
Reputation: 1764
There are two things to consider here.
If you really must replace the collection with a new collection in the data service data ready callback, then ensure you write the collection setter thus:
public ObservableCollection<ModalityType> ModalityTypes {
get {
return _studyRepository.ModalityTypes;
}
set
{
this.__studyRepository.ModalityTypes = value;
RaisePropertyChanged("ModalityTypes");
{
}
Upvotes: 0
Reputation: 14002
The binding will inspect the property when the form initialises. It will also inspect the property when the PropertyChanged
notification is raised - so you may get two calls to the getter of the property
I'm assuming that your call to get data is a web service call or something? Silverlight is async, so you need to consider that web service calls may return well after your form is initialised
Upvotes: 1