Im creating a Login using a window control
to allow a user to login into a WPF
application that I am creating.
So far, I have created a method that checks whether the user has entered in the correct credentials for the username
and password
in a textbox
on the login screen, binding
two properties
.
I have achieved this by creating a bool
method, like so;
public bool CheckLogin()
{
var user = context.Users.Where(i => i.Username == this.Username).SingleOrDefault();
if (user == null)
{
MessageBox.Show("Unable to Login, incorrect credentials.");
return false;
}
else if (this.Username == user.Username || this.Password.ToString() == user.Password)
{
MessageBox.Show("Welcome " + user.Username + ", you have successfully logged in.");
return true;
}
else
{
MessageBox.Show("Unable to Login, incorrect credentials.");
return false;
}
}
public ICommand ShowLoginCommand
{
get
{
if (this.showLoginCommand == null)
{
this.showLoginCommand = new RelayCommand(this.LoginExecute, null);
}
return this.showLoginCommand;
}
}
private void LoginExecute()
{
this.CheckLogin();
}
I also have a command
that I bind
to my button within the xaml
like so;
<Button Name="btnLogin" IsDefault="True" Content="Login" Command="{Binding ShowLoginCommand}" />
When I enter in the username and password it executes the appropriated code, whether it being right, or wrong. But how can I close this window from the ViewModel when both username and password are correct?
I have previously tried using a dialog modal
but it didn't quite work out. Furthermore, within my app.xaml, I have done something like the following, which loads the login page first, then once true, loads the actual application.
private void ApplicationStart(object sender, StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
var dialog = new UserView();
if (dialog.ShowDialog() == true)
{
var mainWindow = new MainWindow();
Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
Current.MainWindow = mainWindow;
mainWindow.Show();
}
else
{
MessageBox.Show("Unable to load application.", "Error", MessageBoxButton.OK);
Current.Shutdown(-1);
}
}
Question: How can I close the Login Window control
from the ViewModel?
Thanks in advance.
Best Solution
You can pass the window to your ViewModel using the
CommandParameter
. See my Example below.I've implemented an
CloseWindow
Method which takes a Windows as parameter and closes it. The window is passed to the ViewModel viaCommandParameter
. Note that you need to define anx:Name
for the window which should be close. In my XAML Window i call this method viaCommand
and pass the window itself as a parameter to the ViewModel usingCommandParameter
.ViewModel
View
Note that i'm using the MVVM light framework, but the principal applies to every wpf application.
This solution violates of the MVVM pattern, because the view-model shouldn't know anything about the UI Implementation. If you want to strictly follow the MVVM programming paradigm you have to abstract the type of the view with an interface.
MVVM conform solution (Former EDIT2)
the user Crono mentions a valid point in the comment section:
You can fix this by introducing an interface containing a close method.
Interface:
Your refactored ViewModel will look like this:
ViewModel
You have to reference and implement the
ICloseable
interface in your viewView (Code behind)
Answer to the original question: (former EDIT1)
Your Login Button (Added CommandParameter):
Your code: