Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / WPF

WPF/Silverlight: Step By Step Guide to MVVM

Rate me:
Please Sign up or sign in to vote.
4.66/5 (26 votes)
19 Jul 2011CPOL8 min read 110.7K   5.4K   103  
This article aims to provide basic overview of MVVM design pattern which is very popular amongst WPF/Silverlight application developers. This is a very basic practical tutorial and aims at providing a step by step guide to people who are new to MVVM.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace MVVMDemo.Model
{
    public class Employee : IDataErrorInfo, INotifyPropertyChanged
    {
        #region Constructor
        /// <summary>
        /// Constructor for Employee class with default parameters
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="age"></param>
        /// <param name="notes"></param>
        public Employee(string id = "", string name = "", uint age = 0, string notes = "")
        {
            _id = id;
            _name = name;
            _age = age;
            _notes = notes;
        } 
        #endregion

        #region Properties
        private string _id = string.Empty;

        public string ID
        {
            get { return _id; }
            set
            {
                _id = value;
                RaisePropertyChanged("ID");
            }
        }

        private string _name = string.Empty;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }

        private uint _age = 0;

        public uint Age
        {
            get { return _age; }
            set
            {
                _age = value;
                RaisePropertyChanged("Age");
            }
        }

        private string _notes = string.Empty;

        public string Notes
        {
            get { return _notes; }
            set
            {
                _notes = value;
                RaisePropertyChanged("Notes");
            }
        } 
        #endregion

        #region IDataErrorInfo
        public string Error
        {
            get
            {
                return this[string.Empty];
            }
        }

        public string this[string propertyName]
        {
            get
            {

                string result = string.Empty;
                propertyName = propertyName ?? string.Empty;

                if (propertyName == string.Empty || propertyName == "ID")
                {
                    if (string.IsNullOrEmpty(this.ID))
                    {
                        result = "Employee ID is invalid. ID cannot be null or blank";
                    }
                    else if (System.Text.RegularExpressions.Regex.IsMatch(this.ID, "[/!@#?/}[}{ ]"))
                    {
                        result = "Employee ID is invalid. ID cannot have special characters";
                    }
                }
                else if (propertyName == "Name")
                {
                    if (string.IsNullOrEmpty(this.Name))
                    {
                        result = "Name is invalid. ID cannot be null or blank";
                    }
                }
                else if (propertyName == "Age")
                {
                    if (Age > 150 || Age < 0)
                    {
                        result = "Age is invalid. Age should be between 0 and 150";
                    }
                }

                return result;
            }
        }
        #endregion

        #region INotifyPropertyChanged
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

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
Technical Lead Barclays Capital
Singapore Singapore
Pradeep Dhawan:
Worked on Prism, Smart Client(SCSF CAB), WPF, WCF, WF, C#, VC++, C and Unix shell scripting.

Blog: http://programmingwpf.blogspot.com

Comments and Discussions