I am using WPF with the model-view-viewmodel template. I have ResultsView and ResultsViewModel that are related as follows:
<DataTemplate DataType="{x:Type VM:ResultsViewModel}"> <VW:ResultsView/> </DataTemplate>
In my main window, I have some kind of page setup where the MainContent property stores the current page (an instance of ViewModel).
I create the page as follows
MainContent = new ResultsViewModel();
ResultsView is based on UserControl, it also has a handler for the Loaded event, which performs some user interface initialization operations.
Everything works fine when the user moves between different pages.
But if the user opens the ResultsView line twice, the ResultsView constructor references not for the second time, and the Loaded event is not raised. It looks like I now have the same old instance of ResultsView that is bound to the new ResultsViewModel ()!
Why doesn't WPF create a new view every time I create a new ViewModel? Is there a way to force WPF to abandon the old view if the old view model is destroyed?
source share