Click here to Skip to main content
15,886,030 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.5K   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.Web
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ServiceModel.DomainServices.Client;
    using System.ServiceModel.DomainServices.Client.ApplicationServices;
    using WindowWithMVVM.Web.Resources;

    /// <summary>
    /// Extensions to provide client side custom validation and data binding to <see cref="RegistrationData"/>.
    /// </summary>
    public partial class RegistrationData
    {
        private OperationBase currentOperation;

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

        /// <summary>
        /// Gets and sets the password.
        /// </summary>
        [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [Display(Order = 3, Name = "PasswordLabel", Description = "PasswordDescription", ResourceType = typeof(RegistrationDataResources))]
        [RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [StringLength(50, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        public string Password
        {
            get
            {
                return (this.PasswordAccessor == null) ? string.Empty : this.PasswordAccessor();
            }

            set
            {
                this.ValidateProperty("Password", value);
                this.CheckPasswordConfirmation();

                // 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 or sets a function that returns the password confirmation.
        /// </summary>
        internal Func<string> PasswordConfirmationAccessor { get; set; }

        /// <summary>
        /// Gets and sets the password confirmation string.
        /// </summary>
        [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
        [Display(Order = 4, Name = "PasswordConfirmationLabel", ResourceType = typeof(RegistrationDataResources))]
        public string PasswordConfirmation
        {
            get
            {
                return (this.PasswordConfirmationAccessor == null) ? string.Empty : this.PasswordConfirmationAccessor();
            }

            set
            {
                this.ValidateProperty("PasswordConfirmation", value);
                this.CheckPasswordConfirmation();

                // 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("PasswordConfirmation");
            }
        }

        /// <summary>
        /// Gets or sets the current registration or login operation.
        /// </summary>
        internal OperationBase CurrentOperation
        {
            get
            {
                return this.currentOperation;
            }
            set
            {
                if (this.currentOperation != value)
                {
                    if (this.currentOperation != null)
                    {
                        this.currentOperation.Completed -= (s, e) => this.CurrentOperationChanged();
                    }

                    this.currentOperation = value;

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

                    this.CurrentOperationChanged();
                }
            }
        }

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

        /// <summary>
        /// Helper method for when the current operation changes.
        /// Used to raise appropriate property change notifications.
        /// </summary>
        private void CurrentOperationChanged()
        {
            this.RaisePropertyChanged("IsRegistering");
        }

        /// <summary>
        /// Checks to ensure the password and confirmation match.
        /// If they don't match, a validation error is added.
        /// </summary>
        private void CheckPasswordConfirmation()
        {
            // If either of the passwords has not yet been entered, then don't test for equality between the fields.
            // The Required attribute will ensure a value has been entered for both fields.
            if (string.IsNullOrWhiteSpace(this.Password)
                || string.IsNullOrWhiteSpace(this.PasswordConfirmation))
            {
                return;
            }

            // If the values are different, then add a validation error with both members specified
            if (this.Password != this.PasswordConfirmation)
            {
                this.ValidationErrors.Add(new ValidationResult(ValidationErrorResources.ValidationErrorPasswordConfirmationMismatch, new string[] { "PasswordConfirmation", "Password" }));
            }
        }

        /// <summary>
        /// Perform logic after the UserName value has been entered.
        /// </summary>
        /// <param name="userName">The user name value that was entered.</param>
        /// <remarks>
        /// Allow the form to indicate when the value has been completely entered.
        /// Using the OnUserNameChanged method can lead to a premature call before the user has finished entering the value in the form.
        /// </remarks>
        internal void UserNameEntered(string userName)
        {
            // Auto-Fill FriendlyName to match UserName for new entities when there is not a friendly name specified
            if (string.IsNullOrWhiteSpace(this.FriendlyName))
            {
                this.FriendlyName = userName;
            }
        }

        /// <summary>
        /// Creates a new <see cref="LoginParameters"/> initialized with this entity's data (IsPersistent will default to false).
        /// </summary>
        public LoginParameters ToLoginParameters()
        {
            return new LoginParameters(this.UserName, this.Password, false, 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