Click here to Skip to main content
12,406,795 members (54,912 online)
Click here to Skip to main content

Stats

1.6M views
49.8K downloads
551 bookmarked
Posted

WPF/MVVM Quick Start Tutorial

, 10 Oct 2012 CPOL
A quick example of MVVM in WPF
MvvmExample
Example1
Properties
Example2
Properties
Example3
Properties
Example4
Properties
Example5
Properties
Example6
Properties
MicroMvvm
Properties
MvvmExample.suo
using System.ComponentModel;
using System.Windows.Input;

namespace Example3
{
    /// <summary>
    /// This class is a view model of a song.
    /// </summary>
    public class SongViewModel : INotifyPropertyChanged
    {
        #region Construction
        /// <summary>
        /// Constructs the default instance of a SongViewModel
        /// </summary>
        public SongViewModel()
        {
            _song = new Song { ArtistName = "Unknown", SongTitle = "Unknown" };
        }
        #endregion

        #region Members
        Song _song;
        int _count = 0;
        #endregion

        #region Properties
        public Song Song
        {
            get
            {
                return _song;
            }
            set
            {
                _song = value;
            }
        }

        public string ArtistName
        {
            get { return Song.ArtistName; }
            set 
            {
                if (Song.ArtistName != value)
                {
                    Song.ArtistName = value;
                    RaisePropertyChanged("ArtistName");
                }
            }
        } 
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region Commands
        void UpdateArtistNameExecute()
        {
            ++_count;
            ArtistName = string.Format("Elvis ({0})", _count);
        }

        bool CanUpdateArtistNameExecute()
        {
            return true;
        }

        public ICommand UpdateArtistName { get { return new RelayCommand(UpdateArtistNameExecute, CanUpdateArtistNameExecute); } }
        #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)

Share

About the Author

Barry Lapthorn
United Kingdom United Kingdom
Jack of all trades.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 10 Oct 2012
Article Copyright 2011 by Barry Lapthorn
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid