Hi,
I am quite new to MVVM, and after searching your support center and the web, I still have problems to find a solution to this:
Basicly I want to show a LoginView with a viewmodel that checks the login data using a WCF service (this part is working).
The method of the WCF service returns a boolean if the login data was correct or not.
In case the login was correct, I want to switch the LoginView to my MainView.
My problem is, that I do not know, how to correctly submit the return value of my WCF service from my LoginViewViewModel to my MainWindowViewModel.
What I have done so far follows here:
I have a (themed) MainWindow which is defined like this:
XAML<dx:ThemedWindow ...>
<dx:ThemedWindow.DataContext>
<ViewModels:MainWindowViewModel/>
</dx:ThemedWindow.DataContext>
<dx:ThemedWindow.Resources>
<DataTemplate x:Key="LoginViewTemplate" DataType="{x:Type ViewModels:LoginViewModel}">
<Views:LoginView/>
</DataTemplate>
<DataTemplate x:Key="MainViewTemplate" DataType="{x:Type ViewModels:MainViewViewModel}">
<Views:MainView/>
</DataTemplate>
</dx:ThemedWindow.Resources>
<Grid>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource LoginViewTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsLoggedIn}" Value="true">
<Setter Property="ContentTemplate" Value="{StaticResource MainViewTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>
</dx:ThemedWindow>
My MainWindowViewModel pretty much only contains a boolean "IsLoggedIn" and an object "CurrentView"
My LoginViewViewModel:
C#class LoginViewModel : ViewModelBase
{
private bool _loginSuccess;
public bool LoginSuccess
{
get { return _loginSuccess; }
set
{
SetProperty(ref _loginSuccess, value, () => LoginSuccess);
}
}
// Constructor
public LoginViewModel()
{
LoginCommand = new DelegateCommand(LoginCommandExecute);
}
// Command Definitions
public DelegateCommand LoginCommand { get; set; }
// Command Methods
private void LoginCommandExecute()
{
if (ConnectionManager.Connection.IsLoginValid(_loginUser, _loginPassword))
{
// WHAT TO DO HERE TO SWITCH THE VIEW IN THE MAIN WINDOW?
}
else
{
// No login for you :/
}
}
}
With the content control in the MainWindow I can switch between the two views when I toggle the "IsLoggedIn" boolean (manually). But how do I toggle this from the LoginViewViewModel class? Is this a good approach at all?
Thanks in advance and many greetings,
Sebastian