Click here to Skip to main content
15,883,705 members
Articles / Desktop Programming / XAML

A Sample Silverlight 4 Application Using MEF, MVVM, and WCF RIA Services - Part 1

Rate me:
Please Sign up or sign in to vote.
4.84/5 (108 votes)
7 Jul 2011CPOL9 min read 2.1M   30.9K   298  
Part 1 of a series describing the creation of a Silverlight business application using MEF, MVVM Light, and WCF RIA Services.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel.Composition;
using GalaSoft.MvvmLight;
using IssueVision.Common;

namespace IssueVision.Client
{
    public partial class LoginForm : UserControl
    {
        #region "Constructor"
        public LoginForm()
        {
            InitializeComponent();

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // Use MEF To load the View Model
                this.DataContext = App.Container.GetExportedValue<ViewModelBase>(
                    ViewModelTypes.LoginFormViewModel);
            }
        }
        #endregion "Constructor"

        #region "Event handlers"

        private void userNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // dynamically enable/disable error message
            if (!string.IsNullOrWhiteSpace(this.loginScreenErrorMessageTextBox.Text))
                this.loginScreenErrorMessageTextBox.Text = string.Empty;

            // dynamically enable/disable login button
            this.loginButton.IsEnabled = !(string.IsNullOrWhiteSpace(this.userNameTextBox.Text) ||
                string.IsNullOrWhiteSpace(this.passwordPasswordBox.Password));
        }

        private void passwordPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            // dynamically enable/disable error message
            if (!string.IsNullOrWhiteSpace(this.loginScreenErrorMessageTextBox.Text))
                this.loginScreenErrorMessageTextBox.Text = string.Empty;

            // dynamically enable/disable login button
            this.loginButton.IsEnabled = !(string.IsNullOrWhiteSpace(this.userNameTextBox.Text) ||
                string.IsNullOrWhiteSpace(this.passwordPasswordBox.Password));
        }

        private void securityAnswerPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            // dynamically enable/disable error message
            if (!string.IsNullOrWhiteSpace(this.resetPasswordScreenErrorMessageTextBox.Text))
                this.resetPasswordScreenErrorMessageTextBox.Text = string.Empty;

            // dynamically enable/disable reset password button
            this.resetPasswordButton.IsEnabled = !(string.IsNullOrWhiteSpace(this.newPasswordPasswordBox.Password) ||
                string.IsNullOrWhiteSpace(this.confirmPasswordPasswordBox.Password) ||
                string.IsNullOrEmpty(this.securityAnswerPasswordBox.Password));
        }

        private void newPasswordPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            // update the ActualPassword every time password changes
            this.ActualPasswordTextBlock.Text = this.newPasswordPasswordBox.Password;

            // dynamically enable/disable error message
            if (!string.IsNullOrWhiteSpace(this.resetPasswordScreenErrorMessageTextBox.Text))
                this.resetPasswordScreenErrorMessageTextBox.Text = string.Empty;

            // dynamically enable/disable reset password button
            this.resetPasswordButton.IsEnabled = !(string.IsNullOrWhiteSpace(this.newPasswordPasswordBox.Password) ||
                string.IsNullOrWhiteSpace(this.confirmPasswordPasswordBox.Password) ||
                string.IsNullOrEmpty(this.securityAnswerPasswordBox.Password));
        }

        private void newPasswordPasswordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            // If confirm password is not empty, we need to re-validate confirm
            // password after leaving password field
            if (!string.IsNullOrEmpty(this.confirmPasswordPasswordBox.Password))
            {
                BindingExpression be = this.confirmPasswordPasswordBox.GetBindingExpression(PasswordBox.PasswordProperty);
                if (be != null)
                    be.UpdateSource();
            }
        }

        private void confirmPasswordPasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            // dynamically enable/disable error message
            if (!string.IsNullOrWhiteSpace(this.resetPasswordScreenErrorMessageTextBox.Text))
                this.resetPasswordScreenErrorMessageTextBox.Text = string.Empty;

            // dynamically enable/disable reset password button
            this.resetPasswordButton.IsEnabled = !(string.IsNullOrWhiteSpace(this.newPasswordPasswordBox.Password) ||
                string.IsNullOrWhiteSpace(this.confirmPasswordPasswordBox.Password) ||
                string.IsNullOrEmpty(this.securityAnswerPasswordBox.Password));
        }

        #endregion "Event handlers"
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions