Say I have ViewModel class MyViewModel like:
public class MyViewModel : ViewModelBase
{
private Person _person;
public Person Person
{
get { return _person; }
set
{
if (this._person != value)
{
this._person = value;
this.RaisePropertyChanged("Person");
}
}
}
private AddNew(){
this = new MyViewMode(new Person());
}
}
What I want is try to create new instance inside this view mode in method AddNew().
When this VM bind to UI, I want to user can to change the viewmodel for new entity without change UI.
But I can’t do that because code this = new MyViewMode(new Person()); won’t work.
How to resolve this problem?
I would recommend not to do something like this…
Google for view-first or viewmodel first…
This is an ongoing discussion with lot’s of opinions which realy makes sense.
http://wildermuth.com/2009/05/22/Which_came_first_the_View_or_the_Model
Further more I would look into the pattern of the ServiceLocator.
Basicaly a service which creates the viewmodels for you…
http://johnpapa.net/silverlight/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/
http://csharperimage.jeremylikness.com/2010/03/viewmodel-binding-with-managed.html