This project has moved and is read-only. For the latest updates, please go here.

Time synchronisation problem between View <-> ViewModel

Mar 26, 2010 at 8:44 AM

Hello everyone,


I have 2 questions for you today :

1. I have a synchronization problem between the creation of my ViewModel and the binding of my View.

I use the ViewModelLocator to set the DataContext of my ItemsView (UserControl) on my ItemsViewModel. The ItemsView is placed in my MainView wich has its own MainViewModel.

My problem is that my ItemsViewModel is instantiate and initialised when the datacontext of my ItemsView is set. After, this one register its binding on ItemsViewModel Properties and listen OnPropertyChanged on an ObservableCollection of ItemsViewModel. On any OnPropertyChanged (add or remove element from the ObservableCollection) my ItemsView has to be updated.

But this collection  is initialised at the ItemsViewModel initialisation and never updated.

 Anybody has a solution to force my view to update/refresh its content ?

2. Is it possible to acces ViewModel properties from the code-behind of my View ? Is it possible to set binding between my View and my ViewModel from the code-behind ?

These questions are very basing and oriented for my project but I think there are based on the MVVM pattern.

Mar 29, 2010 at 10:30 AM

Does the property of type ObservableCollection also raises the PropertyChanged event in the setter?

Mar 29, 2010 at 1:39 PM

ObservableCollection property raises the CollectionChanged event when the collection is updated.

Mar 29, 2010 at 3:00 PM

The property itself should raise the PropertyChanged event.
 See below for a VB example.


Private _ListOfItems As ObservableCollection(Of Items)
Property ListOfItems As ObservableCollection(Of Items)
		Return _ListOfItems
	End Get
	Set(ByVal value As ObservableCollection(Of Items))
		_ListOfItems = value
		RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ListOfItems"))
		'' Or by using the BaseViewModel method
		' RaisePropertyChanged("ListOfItems")
	End Set
End Property