Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / WPF

Closing Windows and Applications with WPF and MVVM

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
10 Aug 2012CPOL12 min read 222.2K   5.8K   66  
This article samples a MVVM conform implementation of startup and shutdown sequences for an application and its dialogs.
namespace DialogCloser.ViewModel
{
  using System.Collections.Generic;

  using DialogCloser.ViewModel.Base;

  /// <summary>
  /// Viewmodel class that manages input states for user who has a first and a last name.
  /// </summary>
  public class UsernameViewModel : ViewModelBase
  {
    #region fields
    private DialogViewModelBase mOpenCloseView;

    private string mFirstName, mLastName;
    #endregion fields

    #region constructor
    /// <summary>
    /// Standard Constructor
    /// </summary>
    public UsernameViewModel()
      : base()
    {
      this.mFirstName = this.mLastName = string.Empty;
      this.DisplayName = "Input a first and last name";
    }

    /// <summary>
    /// Copy constructor
    /// </summary>
    public UsernameViewModel(UsernameViewModel copyThis)
      : this()
    {
      if (copyThis == null) return;

      this.mOpenCloseView = new DialogViewModelBase(copyThis.mOpenCloseView);

      this.mFirstName = copyThis.mFirstName;
      this.mLastName = copyThis.LastName;
    }

    #endregion fields

    #region properties
    /// <summary>
    /// Get property to expose elements necessary to evaluate user input
    /// when the user completes his input (eg.: clicks OK in a dialog).
    /// </summary>
    public DialogViewModelBase OpenCloseView
    {
      get
      {
        return this.mOpenCloseView;
      }
      
      private set
      {
        if (this.mOpenCloseView != value)
        {
          this.mOpenCloseView = value;

          this.NotifyPropertyChanged(() => this.OpenCloseView);
        }
      }
    }

    /// <summary>
    /// Get/set first name of a user
    /// </summary>
    public string FirstName
    {
      get
      {
        return (this.mFirstName == null ? string.Empty : this.mFirstName);
      }

      set
      {
        if (this.mFirstName != value)
        {
          this.mFirstName = value;
          this.NotifyPropertyChanged(() => this.FirstName);
        }
      }
    }

    /// <summary>
    /// Get/set last name of a user
    /// </summary>
    public string LastName
    {
      get
      {
        return (this.mLastName == null ? string.Empty : this.mLastName);
      }

      set
      {
        if (this.mLastName != value)
        {
          this.mLastName = value;
          this.NotifyPropertyChanged(() => this.LastName);
        }
      }
    }
    #endregion properties

    #region methods
    /// <summary>
    /// Initilize input states such that user can input information
    /// with a view based GUI (eg.: dialog)
    /// </summary>
    public void InitDialogInputData()
    {
      this.OpenCloseView = new Base.DialogViewModelBase();

      // Attach delegate method to validate user input on OK
      // Not setting this means that user input is never validated and view will always close on OK
      if (this.mOpenCloseView != null)
        this.mOpenCloseView.EvaluateInputData = this.ValidateData;
    }

    /// <summary>
    /// Delegate method that is call whenever a user OK'es or Cancels the view that is bound to <seealso cref="OpenCloseView"/>
    /// </summary>
    /// <param name="listMsgs"></param>
    /// <returns></returns>
    private bool ValidateData(out List<Msg> listMsgs)
    {
      listMsgs = new List<Msg>();

      const int MinStrLen = 5;

      if (this.FirstName.Trim().Length < MinStrLen)
        listMsgs.Add(new Msg(string.Format("First name must contain at least {0} letters.", MinStrLen), Msg.MsgCategory.Error));

      if (this.LastName.Trim().Length < MinStrLen)
        listMsgs.Add(new Msg(string.Format("Last name must contain at least {0} letters.", MinStrLen), Msg.MsgCategory.Warning));

      return !(listMsgs.Count > 0);
    }
    #endregion methods
  }
}

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
Germany Germany
The Windows Presentation Foundation (WPF) and C# are among my favorites and so I developed Edi

and a few other projects on GitHub. I am normally an algorithms and structure type but WPF has such interesting UI sides that I cannot help myself but get into it.

https://de.linkedin.com/in/dirkbahle

Comments and Discussions