So my app is basically a shell with a ContentControl that I populate with custom UserControl‘s depending on the user selection of a menu.
But now I’m having a weird behaviour.
I had attached the Content property of the ContentControl to a ViewModel property wich I instantiate on demand. That works great but I’m having two problems.
-
When I select one option of the menu it creates a new instance of the specified
UserControland set theContentproperty the shell. That works, cause I see the control and can interact with it, and when I select another option from the menu, it shows me the otherUserControl, but, when I select again the previosly selected option it seems to be loading the same previously instantiated control (the right one but with the older input, and I’m doing a new XXXControl() before setting it as theContentproperty of theContentControl. -
I’m calling a ShowDialog() from inside the custom control via Commands (from the user control view model I call the view via MVVM Light messaging and then show the dialog), and that works. But when I try to close the dialog it show it again the same number of time I select different options from the menu.
For example, I start with A menu and show the dialog, then the close button works, then I goes to B menu and back to A, then the close button works at the second press (two ShowDialog() are being called) and so on…
I don’t know wich part of the code must I paste in order to give a little more of context to this post, but any input will be appreciated. I’m stuck with this (mine) bug.
Code
On the shell view:
<ContentControl Grid.Row="2" Content="{Binding CurrentView}" Margin="15,10"/>
On the shell ViewModel:
if (action == null || action == SEARCH_ACTION)
{
ActionsMenuSelected = SEARCH_ACTION;
var view = new SearchDocumentView();
CurrentView = view;
}
On the inner view (SearchDocumentView):
public SearchDocumentView()
{
InitializeComponent();
Messenger.Default.Register<NotificationMessage<Entity>>(this, NotificationMessageReceived);
}
private void NotificationMessageReceived(NotificationMessage<Entity> msg)
{
if (msg.Notification == "ViewResult")
{
var view = new DocumentViewer( ServiceLocator.Current.GetInstance<IDataService>(),msg.Content);
view.ShowDialog();
}
}
On the inner view (SearchDocumentView) xaml:
<ListBox x:Name="SearchResults" ItemsSource="{Binding SearchResults}" SelectedItem="{Binding SelectedSearchResult}">
<ListBox.InputBindings>
<KeyBinding
Key="Enter"
Command="{Binding ViewResult}" />
<KeyBinding
Key="Return"
Command="{Binding ViewResult}" />
</ListBox.InputBindings>
...
On the inner view (SearchDocumentView) ViewModel:
private RelayCommand _viewResut;
/// <summary>
/// Gets the ViewResult.
/// </summary>
public RelayCommand ViewResult
{
get
{
return _viewResut
?? (_viewResut = new RelayCommand(
() =>
{
MessengerInstance.Send(new NotificationMessage<Entity>((Entity)SelectedSearchResult, "ViewResult"));
},
() => ((Entity)SelectedSearchResult!=null)?true:false ));
}
}
Why you see your old data I cannot see right now – but I guess you use the same (model)-data for the newly created view.
The second problem should be here:
For each new view your register a Notification, that will show your message box but I don’t see if you unregister those and if you don’t the handler will hold the viewmodel in memory and will still show the messages boxes.
Even if I got it wrong (is this your “controls”-viewmodel?) it should be something very similar, but you can find this easily by setting a breakpoint to the
.Showon your message and looking at the callstack while debugging.