I have a main VM which stores 2 UserControl and VM-ки to them. UserControl is UserControl due to the CurrentUserControl property that binds to the view. How correctly to store UserControl inside ViewModel and how best to do it from the MVVM architecture?
Also, to change one UserControl to another, I send a link to the main VM to each VM-ку and it changes them through this link. I also doubt the correctness of this decision.
public class MainVm : VmBase { public MainVm() { LoginAndRegisterVm = new LoginAndRegisterVm(this); EnterRegistrationKeyVm = new EnterRegistrationKeyVm(this); EnterRegistrationKeyUserControl = new EnterRegistrationKey(); EnterRegistrationKeyUserControl.DataContext = EnterRegistrationKeyVm; LoginUserControl.DataContext = LoginAndRegisterVm; CurrentUserControl = LoginUserControl; } public LoginAndRegisterVm LoginAndRegisterVm { get; set; } public EnterRegistrationKeyVm EnterRegistrationKeyVm { get; set; } public UserControl LoginUserControl { get; set; } = new LoginPage(); public UserControl EnterRegistrationKeyUserControl { get; set; } private UserControl _currentUserControl; public UserControl CurrentUserControl { get { return _currentUserControl; } set { _currentUserControl = value; NotifyPropertyChanged(); } } } Part of Xaml :
<UserControl Grid.Row="1" Content="{Binding CurrentUserControl}" />