In fact, there is a much better way to do this using ViewModelViewHost :
<Grid DataContext="{Binding ViewModel, ElementName=TheUserControl}"> <ViewModelViewHost ViewModel="{Binding CurrentControlViewModel}" /> </Grid>
Your class will now look something like this:
public class MainWindowViewModel : ReactiveObject { private ReactiveObject _CurrentControlViewModel = new HomePageViewModel(); public ReactiveObject CurrentControlViewModel { get { return _CurrentControl; } set { this.RaiseAndSetIfChanged(x => x.CurrentControlViewModel, value); } } }
And somewhere in your application run, you should write:
RxApp.Register(typeof(IViewFor<HomePageViewModel>), typeof(HomePage));
What is ViewModelViewHost?
ViewModelViewHost will take the ViewModel object that you provide through Bindings and find the view that suits it using Service Location. The Register call is how you can associate Views with ViewModels.
source share