Overview:
I have one MainWindow
and two Views
. One representing a Login Screen and the other one is representing the Dashboard. The LoginView
is shown on StartUp
. When the User presses the LoginButton
the DashboardView
should be shown.
Here's my Problem:
The underlying Command
is beeing executed. The Constructor
of DashboardViewModel
is beeing called. And the View
and ViewModel
for Dashboard are connected via a DataTemplate
. But the View
or the InitializeComponent
Method are not beeing called.
The LoginCommand
looks like this:
LoginMainCommands
is a RelayCommand
Class derived from ICommand
.
public class ButtonViewModel : LoginMainViewModel
{
public ButtonViewModel()
{
_loginCommand = new LoginMainCommands(Login);
}
private LoginMainCommands _loginCommand;
public LoginMainCommands LoginCommand
{
get { return _loginCommand; }
}
public void Login()
{
ViewModelLocator ObjViewModelLocator = new ViewModelLocator();
ObjViewModelLocator.MWInstance.SwitchViewModel();
}
}
The connection between the Views
and the ViewModels
is in ManWindow.xaml
as follows:
<Window.Resources>
<local:ViewModelLocator x:Key="ViewModelLocator"/>
<DataTemplate DataType="{x:Type loginViewModel:LoginMainViewModel}">
<loginView:LoginMainViewUserControl/>
</DataTemplate>
<DataTemplate DataType="{x:Type dashboardViewModel:DashboardMainViewModel}">
<dashboardViews:DashboardMainViewUserControl/>
</DataTemplate>
</Window.Resources>
To switch between the Views
I added this Method in MainWindowViewModel
:
public void SwitchViewModel()
{
if (!isLoginView)
{
isLoginView = true;
ViewModel = new LoginMainViewModel();
}
else
{
isLoginView = false;
ViewModel = new DashboardMainViewModel();
}
}
What I've tried so far:
I did almost everything this answer suggests. But I can't connect the Views
and ViewModels
in the App.xaml
, cause then I can't use my ResourceDictionaries
for Icons and Logos. But they are connected in MainWindow.xaml
.
Later on I recognized in order for this to work only one Instance
of MainWindowViewModel
could exist because otherwise ViewModel
would be null
everytime a new Object
is created. That's why I created ViewModelLocator
like this answer suggests.
The weird part for me is when I change the if
bracket in the SwitchViewModel
Method to this:
if (!isLoginView)
{
isLoginView = true;
ViewModel = new DashboardMainViewModel();
}
Now DashboardMainViewModel
is the default View
to show and it does excatcly that it shows up.
I have no clue why the Dashboard Screen is not beeing shown.
I want to thank everybody in advance for your help and patience!