Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#

Open Child Window in MVVM environment

Rate me:
Please Sign up or sign in to vote.
4.56/5 (10 votes)
14 May 2012CPOL2 min read 60.4K   1.7K   12  
How to open pop up window in MVVM environment. You can use it in WPF or Silverlight.
  • WindowWithMVVM.zip
    • WindowWithMVVM
      • WindowWithMVVM.sln
      • WindowWithMVVM.suo
      • WindowWithMVVM.Web
      • WindowWithMVVM
        • App.xaml
        • App.xaml.cs
        • Assets
        • Bin
          • Debug
            • AppManifest.xaml
            • ar
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • bg
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • ca
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • cs
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • da
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • de
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • el
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • es
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • et
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • eu
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • fi
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • fr
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • he
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • hr
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • hu
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • id
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • it
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • ja
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • ko
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • lt
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • lv
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • ms
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • nl
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • no
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • pl
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • pt
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • pt-BR
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • ro
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • ru
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • sk
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • sl
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • sr-Cyrl-CS
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • sr-Latn-CS
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • sv
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • System.ComponentModel.DataAnnotations.dll
            • System.ComponentModel.DataAnnotations.xml
            • System.ServiceModel.DomainServices.Client.dll
            • System.ServiceModel.DomainServices.Client.Web.dll
            • System.ServiceModel.DomainServices.Client.Web.xml
            • System.ServiceModel.DomainServices.Client.xml
            • System.ServiceModel.Web.Extensions.dll
            • System.ServiceModel.Web.Extensions.xml
            • System.Windows.Controls.Data.DataForm.Toolkit.dll
            • System.Windows.Controls.Data.Input.dll
            • System.Windows.Controls.Data.Input.xml
            • System.Windows.Controls.dll
            • System.Windows.Controls.Navigation.dll
            • System.Windows.Controls.Navigation.xml
            • System.Windows.Controls.xml
            • TestPage.html
            • th
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • tr
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • uk
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • vi
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.resources.dll
            • WindowWithMVVM.dll
            • WindowWithMVVM.pdb
            • WindowWithMVVM.xap
            • zh-Hans
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
            • zh-Hant
              • System.ComponentModel.DataAnnotations.resources.dll
              • System.ServiceModel.DomainServices.Client.resources.dll
              • System.ServiceModel.DomainServices.Client.Web.resources.dll
              • System.ServiceModel.Web.Extensions.resources.dll
              • System.Windows.Controls.Data.Input.resources.dll
              • System.Windows.Controls.Navigation.resources.dll
              • System.Windows.Controls.resources.dll
        • Controls
        • Generated_Code
        • GlobalSuppressions.cs
        • Helpers
        • Libs
          • System.Windows.Controls.Data.DataForm.Toolkit.dll
        • MainPage.xaml
        • MainPage.xaml.cs
        • Models
        • obj
        • Properties
        • ViewModel
        • Views
        • Web
          • Resources
        • WindowWithMVVM.csproj
        • WindowWithMVVM.csproj.user
namespace WindowWithMVVM.LoginUI
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ServiceModel.DomainServices.Client;
    using System.ServiceModel.DomainServices.Client.ApplicationServices;
    using WindowWithMVVM.Web.Resources;

    /// <summary>
    /// This internal entity is used to ease the binding between the UI controls (DataForm and the label displaying a validation error) and the log on credentials entered by the user.
    /// </summary>
    public class LoginInfo : ComplexObject
    {
        private string userName;
        private bool rememberMe;
        private LoginOperation currentLoginOperation;

        /// <summary>
        /// Gets and sets the user name.
        /// </summary>
        [Display(Name = "UserNameLabel", ResourceType = typeof(RegistrationDataResources))]
        [Required]
        public string UserName
        {
            get
            {
                return this.userName;
            }

            set
            {
                if (this.userName != value)
                {
                    this.ValidateProperty("UserName", value);
                    this.userName = value;
                    this.RaisePropertyChanged("UserName");
                }
            }
        }

        /// <summary>
        /// Gets or sets a function that returns the password.
        /// </summary>
        internal Func<string> PasswordAccessor { get; set; }

        /// <summary>
        /// Gets and sets the password.
        /// </summary>
        [Display(Name = "PasswordLabel", ResourceType = typeof(RegistrationDataResources))]
        [Required]
        public string Password
        {
            get
            {
                return (this.PasswordAccessor == null) ? string.Empty : this.PasswordAccessor();
            }
            set
            {
                this.ValidateProperty("Password", value);

                // Do not store the password in a private field as it should not be stored in memory in plain-text.
                // Instead, the supplied PasswordAccessor serves as the backing store for the value.

                this.RaisePropertyChanged("Password");
            }
        }

        /// <summary>
        /// Gets and sets the value indicating whether the user's authentication information should be recorded for future logins.
        /// </summary>
        [Display(Name = "RememberMeLabel", ResourceType = typeof(ApplicationStrings))]
        public bool RememberMe
        {
            get
            {
                return this.rememberMe;
            }

            set
            {
                if (this.rememberMe != value)
                {
                    this.ValidateProperty("RememberMe", value);
                    this.rememberMe = value;
                    this.RaisePropertyChanged("RememberMe");
                }
            }
        }

        /// <summary>
        /// Gets or sets the current login operation.
        /// </summary>
        internal LoginOperation CurrentLoginOperation
        {
            get
            {
                return this.currentLoginOperation;
            }
            set
            {
                if (this.currentLoginOperation != value)
                {
                    if (this.currentLoginOperation != null)
                    {
                        this.currentLoginOperation.Completed -= (s, e) => this.CurrentLoginOperationChanged();
                    }

                    this.currentLoginOperation = value;

                    if (this.currentLoginOperation != null)
                    {
                        this.currentLoginOperation.Completed += (s, e) => this.CurrentLoginOperationChanged();
                    }

                    this.CurrentLoginOperationChanged();
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether the user is presently being logged in.
        /// </summary>
        [Display(AutoGenerateField = false)]
        public bool IsLoggingIn
        {
            get
            {
                return this.CurrentLoginOperation != null && !this.CurrentLoginOperation.IsComplete;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the user can presently log in.
        /// </summary>
        [Display(AutoGenerateField = false)]
        public bool CanLogIn
        {
            get
            {
                return !this.IsLoggingIn;
            }
        }

        /// <summary>
        /// Raises operation-related property change notifications when the current login operation changes.
        /// </summary>
        private void CurrentLoginOperationChanged()
        {
            this.RaisePropertyChanged("IsLoggingIn");
            this.RaisePropertyChanged("CanLogIn");
        }

        /// <summary>
        /// Creates a new <see cref="LoginParameters"/> instance using the data stored in this entity.
        /// </summary>
        public LoginParameters ToLoginParameters()
        {
            return new LoginParameters(this.UserName, this.Password, this.RememberMe, null);
        }
    }
}

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
Architect
United States United States
Manoj Kumar

AWS Certified Solutions Architect (Web development, Serverless, DevOps, Data)
MSSE - Cloud and Mobile Computing, San Jose State University

A wide range of experience in resolving complex business problems.

* Cloud Technologies: EC2, S3, DynamoDB & RDS databases, Lambda serverless architecture, Microservices architecture, API Gateway, Cloud Front CDN, Linux/Windows systems administration, CloudFormation, DevOps, Docker, CICD, Node.js, Python, Java and other open source technologies. Familiarity with OpenStack.
* Web Technologies: HTML5, Node.Js, MEAN Stack, AngularJS, ASP.Net Core, MVC5, CSS3, jQuery, Bootstrap, MongoDB, JavaScript, JSON, AJAX.
* Data: Experience in database architecture, Big Data, Machine Learning, BI, Data Analytics, No-SQL databases, ETL.
* Mobile: IOS/Android app development

He lives with his wife Supriya and daughter Tisya in Bay Area.

Comments and Discussions