ContentControl

Topics: Questions
Apr 12, 2013 at 3:04 PM
I'm seeing a behavior in my application with ContentControl that I have yet to find how to fix.

The ContentControl is used to display different user controls. Its content property is bound to a ViewModelBase property.
<ContentControl Content="{Binding CurrentNavigation}">
I then just assign the proper View Model to the CurrentNavigation property to display the user control I need.
CurrentNavigation = new SensorNavigationViewModel();
This works fine the first time I display content. When I need to change the displayed user control, I again reassign the proper View Model.
CurrentNavigation = new AdministrationNavigationViewModel();
Now this seems to work fine, everything is displayed as it should. However the previous View Model gets created momentarily by the framework. See the traces in the followings messages as it didn't fit in here.

It's this creation behavior I'm trying to stop.

Thanks
Apr 12, 2013 at 3:05 PM
The traces first part:
10:33:46:633 => [DEBUG] [Catel.MVVM.ViewModelBase] Creating view model of type 'AdministrationNavigationViewModel' with unique identifier 3
10:33:46:635 => [DEBUG] [Catel.MVVM.ViewModelBase] Using the default instance of the IServiceLocator
10:33:46:635 => [DEBUG] [Catel.MVVM.ViewModelBase] Registering view model services
10:33:46:636 => [DEBUG] [Catel.MVVM.ViewModelServiceHelper] Registering default service implementations for IoC container
10:33:46:636 => [DEBUG] [Catel.MVVM.ViewModelServiceHelper] Registered default service implementations for IoC container
10:33:46:637 => [DEBUG] [Catel.MVVM.ViewModelBase] Registered view model services
10:33:46:637 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Creating a ViewModelCommandManager for view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:638 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:638 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:639 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Created a ViewModelCommandManager for view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
...
10:33:46:686 => [DEBUG] [Catel.MVVM.ManagedViewModel] Added view model instance, currently containing '1' instances of type 'Manager.ViewModels.AdministrationNavigationViewModel'
10:33:46:687 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:687 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:688 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.AdministrationNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has changed a property ('Groups')
10:33:46:688 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:689 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:690 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:690 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:691 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.AdministrationNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has changed a property ('IsDirty')
10:33:46:691 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:33:46:692 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
The thread '<No Name>' (0x1e50) has exited with code 0 (0x0).
...
10:40:45:039 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:40:45:044 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Found command 'NavigateToDetails' on view model 'AdministrationNavigationViewModel'
10:40:45:050 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.AdministrationNavigationViewModel' with unique identifier '3'
10:40:45:075 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.AdministrationNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has changed a property ('Groups')
10:40:45:077 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.AdministrationNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has changed a property ('Groups')
10:40:45:078 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.LogicBase] DataContext of TargetControl 'SensorNavigation' has changed to 'null'
10:40:45:081 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.SensorNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has raised an event ('Canceling')
Manager.vshost.exe Information: 0 : 10:40:45:082 => [INFO] [Catel.MVVM.ViewModelBase] Canceled view model 'Manager.ViewModels.SensorNavigationViewModel'
Manager.vshost.exe Information: 0 : 10:40:45:083 => [INFO] [Catel.MVVM.ViewModelBase] Canceled view model 'Manager.ViewModels.SensorNavigationViewModel'
10:40:45:084 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.SensorNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has raised an event ('Canceled')
10:40:45:085 => [DEBUG] [Catel.MVVM.ViewModelManager] Unregistering all models of view model 'SensorNavigationViewModel' (id = '2')
10:40:45:086 => [DEBUG] [Catel.MVVM.ViewModelManager] Unregistered all '0' models of view model 'SensorNavigationViewModel' (id = '2')
10:40:45:087 => [DEBUG] [Catel.Messaging.MessageMediator] Unregistered '0' handlers for the recipient with tag 'null'
10:40:45:090 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Unregistering commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '2'
10:40:45:090 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Unregistered commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '2'
10:40:45:091 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Disposing commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '2'
10:40:45:092 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Disposed commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '2'
10:40:45:093 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.SensorNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has raised an event ('Closed')
Apr 12, 2013 at 3:05 PM
The traces second part:
Manager.vshost.exe Information: 0 : 10:40:45:094 => [INFO] [Catel.MVVM.ViewModelBase] Closed view model 'Manager.ViewModels.SensorNavigationViewModel'
10:40:45:095 => [DEBUG] [Catel.MVVM.ManagedViewModel] Removed view model instance, currently containing '0' instances of type 'Manager.ViewModels.SensorNavigationViewModel'
10:40:45:096 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initializing view model 'null'
10:40:45:097 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitializing view model 'SensorNavigationViewModel'
10:40:45:097 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Uninitialized view model 'SensorNavigationViewModel'
10:40:45:098 => [DEBUG] [Catel.MVVM.Views.ViewToViewModelMappingHelper] Initialized view model 'null'
10:40:45:101 => [DEBUG] [Catel.Windows.Controls.MVVMProviders.Logic.LogicBase] Using IViewModelFactory 'Catel.MVVM.ViewModelFactory' to instantiate the view model
10:40:45:102 => [DEBUG] [Catel.IoC.TypeFactory] No constructor could be used, using Activator.CreateInstance. Cannot cache this either.
10:40:45:102 => [DEBUG] [Catel.MVVM.ViewModelBase] Creating view model of type 'SensorNavigationViewModel' with unique identifier 4
10:40:45:103 => [DEBUG] [Catel.MVVM.ViewModelBase] Using the default instance of the IServiceLocator
10:40:45:103 => [DEBUG] [Catel.MVVM.ViewModelBase] Registering view model services
10:40:45:104 => [DEBUG] [Catel.MVVM.ViewModelServiceHelper] Registering default service implementations for IoC container
10:40:45:104 => [DEBUG] [Catel.MVVM.ViewModelServiceHelper] Registered default service implementations for IoC container
10:40:45:105 => [DEBUG] [Catel.MVVM.ViewModelBase] Registered view model services
10:40:45:105 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Creating a ViewModelCommandManager for view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:106 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:106 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:107 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Created a ViewModelCommandManager for view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:107 => [DEBUG] [Catel.MVVM.ViewModelBase] Metadata for view model 'Manager.ViewModels.SensorNavigationViewModel' is already determined once, re-using existing metadata
...
10:40:45:133 => [DEBUG] [Catel.MVVM.ManagedViewModel] Added view model instance, currently containing '1' instances of type 'Manager.ViewModels.SensorNavigationViewModel'
10:40:45:133 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:134 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:134 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.SensorNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has changed a property ('Groups')
10:40:45:135 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:135 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:136 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:136 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:137 => [DEBUG] [Catel.MVVM.ViewModelBase] A view model ('Manager.ViewModels.SensorNavigationViewModel') the current view model ('Manager.ViewModels.MainWindowViewModel') is interested in has changed a property ('IsDirty')
10:40:45:137 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registering commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
10:40:45:138 => [DEBUG] [Catel.MVVM.ViewModelCommandManager] Registered commands on view model 'Manager.ViewModels.SensorNavigationViewModel' with unique identifier '4'
Coordinator
Apr 12, 2013 at 3:22 PM
Can you put this into a real repro for us? I cannot determine what goes wrong exactly. What you are doing seems fine, but there must be a SensorNavigationView that is causing the new VM to be created.
Apr 15, 2013 at 1:58 PM
I added the sample to reproduce in the issue tracker:

Link

Great framework you have there by the way, I've been using it for about two months and really like how simple yet powerful it is.
Coordinator
Apr 15, 2013 at 3:46 PM
Thank you very much for the reproducable app. This makes it much easier for us to debug.

And thanks for the kind words :-)