I've spent quite some time to try and find an elegant solution for the following challenge. I've been unable to find a solution that's more than a hack around the problem.
I've got a simple setup of a View, ViewModel and a Model. I will keep it very simple for the sake of explanation.
- The
Model
has a single property calledTitle
of type String. - The
Model
is the DataContext for theView
. - The
View
has aTextBlock
thats databound toTitle
on the Model. - The
ViewModel
has a method calledSave()
that will save theModel
to aServer
- The
Server
can push changes made to theModel
So far so good. Now there are two adjustments I need to make in order to keep the Model in sync with a Server
. The type of server is not important. Just know that I need to call Save()
in order to push the Model to the Server.
Adjustment 1:
- The
Model.Title
property will need to callRaisePropertyChanged()
in order to translate changes made to theModel
by theServer
to theView
. This works nicely since theModel
is the DataContext for theView
Not too bad.
Adjustment 2:
- Next step is to call
Save()
to save changes made from theView
to theModel
on theServer
. This is where I get stuck. I can handle theModel.PropertyChanged
event on theViewModel
that calls Save() when the Model gets changed but this makes it echo changes made by the Server.
I'm looking for an elegant and logical solution and am willing to change my architecture if it makes sense.