Click here to Skip to main content
15,891,761 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hiya,

I am very new to WPF having done some development work in Win Forms previously, and was wondering, how am I able to do a login procedure using WPF with MVVC?

What I want to do is have a login dialogue appear and if the login procedure is successful, then the user should be redirected to the main application. If they are not, they should simply be able to either enter their details again, or close the application.

Any help is very much appreciated!
Posted

Try Google[^] first.
Got About 216,000 results in 0.53 seconds!!

Simple WPF Login demo, with MVVM pattern[^]
This is the first link from that search, it is with source.
 
Share this answer
 
Comments
Scrappy 1871 14-Jun-12 9:33am    
The reason I am asking the question on here is because I already have that solution and as a result, do not know how to go from there to the other part of my application.
Prasad_Kulkarni 14-Jun-12 9:35am    
That's really great that you've tried something on your question before. So, post the code snippets, and let us know the problem where exactly you get stuck'd.
Scrappy 1871 14-Jun-12 9:53am    
See Solution 2 :)
This is my login xaml file (View):

XML
<page x:class="WolfswoodInCinch.Login" xmlns:x="#unknown">
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:CinchV2="clr-namespace:Cinch;assembly=Cinch.WPF"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
	 xmlns:views="clr-namespace:WolfswoodInCinch.Views"
        Title="Wolfswood Coach Booking System" Height="350" Width="525"
        xmlns:meffed="http:\\www.codeplex.com\MEFedMVVM"
        meffed:ViewModelLocator.ViewModel="LoginViewModel">

    <grid height="193" width="236">
        <label content="Wolfswood Coach Booking Application" height="28" horizontalalignment="Left" margin="10,10,0,0" name="label1" verticalalignment="Top" />
        <label content="Login" height="28" horizontalalignment="Left" margin="10,31,0,0" name="label2" verticalalignment="Top" />
        <textblock text="{Binding Path=Error, Mode=TwoWay}" foreground="Red"></textblock>
        <label content="Username: " height="28" horizontalalignment="Left" margin="10,78,0,0" name="label3" verticalalignment="Top" />
        <label content="Password: " height="28" horizontalalignment="Left" margin="10,112,0,0" name="label4" verticalalignment="Top" />

        <textbox height="23" horizontalalignment="Left" margin="82,83,0,0" name="textBox1" verticalalignment="Top" width="120" text="{Binding UserName.DataValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}">
            <i:interaction.behaviors xmlns:i="#unknown">
                <cinchv2:textboxfocusbehavior isusingdatawrappers="True" xmlns:cinchv2="#unknown" />
                <cinchv2:textboxfocusbehavior xmlns:cinchv2="#unknown" />
            </i:interaction.behaviors>
        </textbox>

        <textbox height="23" horizontalalignment="Left" margin="82,117,0,0" name="passwordBox1" verticalalignment="Top" width="120" text="{Binding Password.DataValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}">
            <i:interaction.behaviors xmlns:i="#unknown">
                <cinchv2:textboxfocusbehavior isusingdatawrappers="True" xmlns:cinchv2="#unknown" />
                <cinchv2:textboxfocusbehavior xmlns:cinchv2="#unknown" />
            </i:interaction.behaviors>
        </textbox>

        <button content="Login" isdefault="True" height="23" horizontalalignment="Left" margin="149,158,0,0" name="button1" command="{Binding LoginViewFiredCommand}" verticalalignment="Top" width="75" click="button1_Click">
        </button>
        <textblock text="{Binding Path=loginResult}" horizontalalignment="Left" margin="12,146,0,12" name="textBlock1" width="131" />
    </grid>
</page>


And this is my MV class:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.ComponentModel;
using WolfswoodInCinch.Views;
using WolfswoodInCinch.Classes;
using WolfswoodInCinch.Commands;
using MEFedMVVM.ViewModelLocator;
using Cinch;
using System.ComponentModel.Composition;
using System.Windows.Navigation;


namespace WolfswoodInCinch.ViewModels
{
    [ExportViewModel("LoginViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class LoginViewModel : ViewModelBase
    {
        private DataWrapper<string> userName;
        private DataWrapper<string> password;
        private IEnumerable<datawrapperbase> cachedListOfDataWrappers;
        //public IUIVisualizerService uiVisualizer;

        public LoginWindow Parent { get; set; }

        [ImportingConstructor]
        public LoginViewModel(/*IUIVisualizerService uiVisualizer*/)
        {
            UserName = new DataWrapper<string>(this, userNameChangeArgs);
            UserName.IsEditable = true;

            Password = new DataWrapper<string>(this, passwordChangeArgs);
            Password.IsEditable = true;

            cachedListOfDataWrappers = DataWrapperHelper.GetWrapperProperties<loginviewmodel>(this);


            //this.uiVisualizer = uiVisualizer;
            LoginViewFiredCommand = new SimpleCommand<object,>(ExecuteLoginCommand);
        }
  
        /// <summary>
        /// Username
        /// </summary>
        /// 
        static PropertyChangedEventArgs userNameChangeArgs = ObservableHelper.CreateArgs<loginviewmodel>(y => y.UserName);

        public DataWrapper<string> UserName
        {
            get { return userName; }
            private set
            {
                userName = value;
                NotifyPropertyChanged(userNameChangeArgs);
            }
        }

        /// <summary>
        /// Password
        /// </summary>
        static PropertyChangedEventArgs passwordChangeArgs = ObservableHelper.CreateArgs<loginviewmodel>(x => x.Password);

        public DataWrapper<string> Password
        {
            get { return password; }
            private set
            {
                password = value;
                NotifyPropertyChanged(passwordChangeArgs);
            }
        }

        public SimpleCommand<object,> LoginViewFiredCommand { get; private set; }

        private void ExecuteLoginCommand(EventToCommandArgs args)
        {
            
            string username = UserName.DataValue;
            string password = Password.DataValue;

            loginResult = "Successful login";

            //((LoginWindow)args.Sender).DialogResult = true;

            
            //Home home = new Home();
            //Welcome welcome = new Welcome();
            //welcome.Visibility.Equals(true);
            //Welcome main = new Welcome();
            //this.
            //this.Close();
            //WorkspaceData workspace = new WorkspaceData(
            //Views.Add(home);
            //NavigationService.GetNavigationService(this).Navigate(home);
        }

        string result;

        public string loginResult
        {
            get { return result; }
            set
            {
                result = value;
                NotifyPropertyChanged(_loginResultChangeArgs);
            }
        }

        static PropertyChangedEventArgs _loginResultChangeArgs = ObservableHelper.CreateArgs<loginviewmodel>(x => x.loginResult);

    }
}

</loginviewmodel></string></loginviewmodel></string></loginviewmodel></loginviewmodel></string></string></datawrapperbase></string></string>


What I want to be able to do is in the ViewModel's ExecuteLoginCommand method, close the window if the user has successfully logged in, and then view the main application. I appreciate at present there isn't any data validation, this will come once the fundamentals have been implemented.
 
Share this answer
 
Comments
Sandeep Mewara 14-Jun-12 10:36am    
This is not an answer. Please use 'Have a Question or Comment' link to respond to an answer. It will notify answerer of your comment such that he can come back and reply.

if you have long code part, use 'Improve Question' link to edit/update your question with it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900