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 called Title
of type String.
- The
Model
is the DataContext for the View
.
- The
View
has a TextBlock
thats databound to Title
on the Model.
- The
ViewModel
has a method called Save()
that will save the Model
to a Server
- The
Server
can push changes made to the Model
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 call RaisePropertyChanged()
in order to translate changes made to the Model
by the Server
to the View
. This works nicely since the Model
is the DataContext for the View
Not too bad.
Adjustment 2:
- Next step is to call
Save()
to save changes made from the View
to the Model
on the Server
. This is where I get stuck. I can handle the Model.PropertyChanged
event on the ViewModel
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…