I just recognized that, each time when i use MVVM and try to show a ViewModel with its View, a instance of the View will be created, although i just use the same ViewModel.
In my View is a GridView. I wrote this in the code-behind to call the event of ViewModel.
private void gridView_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
DataContext.CallOnClick((DataContext as IHasSelectedItem<IViewModel>)?.SelectedItem);
}
The problem with that is, if i defined some events in code-behind of the View, i will get the events more than once, because instances of the View are always be created and each instance sends the ViewModel the event.
Is there somebody, who has this problem, too. Or somebody knows the solution? Thanks a lot!
-------------------------------------------------------------
Add: I have changed my code-behind like that. It works, i got the event only once. But i am not sure, whether the other views have been disposed.
public TestView()
{
InitializeComponent();
IsVisibleChanged += TestView_IsVisibleChanged;
}
private void TestView_IsVisibleChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
{
if (e.NewValue.Equals(false))
{
MyGridView.SelectionChanged -= gridView_SelectionChanged;
}
else
{
MyGridView.SelectionChanged += gridView_SelectionChanged;
}
}
private void gridView_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
DataContext.CallOnClick((DataContext as IHasSelectedItem<IViewModel>)?.SelectedItem);
}