I'm bit confused with the behavior of my code. I'm still newbee in MVVM light.
I have NewMessageWindow which is shown by command
private ICommand newMessageCommand;
public ICommand NewMessageCommand
{
get
{
if (newMessageCommand == null)
newMessageCommand = new RelayCommand(() =>
{
new NewMessageWindow().Show();
});
return newMessageCommand;
}
}
There can be multiple NewMessageWindows and each of them should have separate ViewModel. But I've noticed when I open multiple windows and if I change something in them it affects all windows. For example when I change combobox then combox values changes in all windows.
How to avoid it? How can I open multiple windows with seprate viewmodels that wouldn't affect each other?
Objects that are changing are ObservableCollections that are bound to view.
Edit:
This is how the ViewLocator looks like
public NewMessageWindowModel NewMessage
{
get
{
return ServiceLocator.Current.GetInstance<NewMessageWindowModel>();
}
}
and in constructor
SimpleIoc.Default.Register<NewMessageWindowModel>();
This is how binding looks like:
DataContext="{Binding NewMessage,
Source={StaticResource Locator}}"
I've fixed problem with
ServiceLocator.Current.GetInstance<NewMessageWindowModel(System.Guid.NewGuid().ToString());
but I've read that old instances are cached. How to get rid of them?