I am learning to use MVVM Light and I am making a program which uses database-first model with Entity Framework.
I have a DataGrid which is bound to a ViewModel, which gets data from a Repository.I am trying to make a button which when clicked opens a window, that is bound to its own View Model and lets you add a record to the DataGrid by adding it to the repository, that has an event, called when an item is added so it can update the DataGridViewModel.
Everything here is fine. I open the form, type in the data, press add and the record gets added to the database and the DataGrid gets refreshed automatically by the repository event.
The problem comes when I open more than one "Add Record" windows. When I type something in one of the windows, it appears in the other one.
I am aware that this is happening because both of the instances of the "Add Record" window are bound to the same instance of the View Model.
Is there some way to resolve this problem so every window can be independent? I know this does not look like much of a problem at the moment, but in the future I am planning to use that same window with the same view model to edit records (is that a good approach and can you recommend me some way of passing the record id or the record itself from the DataGrid window to the Add/Edit Record window?).
Thanks in advance! Tell me if I have missed something and I will add it:)
Edit: Ok this is what I came up with:
public EditParentViewModel EditParent
{
get
{
EditParentViewModel editParentViewModelInstance = new EditParentViewModel(ServiceLocator.Current.GetInstance<IParentsRepository>(),
ServiceLocator.Current.GetInstance<IChildrenRepository>(),
ServiceLocator.Current.GetInstance<IBailiffsRepository>());
return editParentViewModelInstance;
}
}
It is working but is this a correct way to supply the repositories to the View Model?