Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / Windows Forms

MVVM (Model-View-ViewModel) Pattern For Windows Form Applications, using C#

Rate me:
Please Sign up or sign in to vote.
4.84/5 (65 votes)
9 May 2013CPOL4 min read 296.9K   18.6K   191  
It's a pattern like MVVM but for WinForms.
using System.ComponentModel;
using System.Collections.ObjectModel;
using CodeProjectWin;

namespace CodeProjectWin
{
    public class ViewModel : INotifyPropertyChanged
    {
        #region Properties

        public string Text { get; set; }

        private ICommand _addToListCommand;
        public ICommand AddToListCommand
        {
            get
            {
                return _addToListCommand ?? (_addToListCommand = new AddToList(this));
            }
        }

        private ICommand _textChangedCommand;
        public ICommand TextChangedCommand
        {
            get
            {
                return _textChangedCommand ?? (_textChangedCommand = new TextChanged(this));
            }
        }

        private ObservableCollection<StringValue> _nameList;
        public ObservableCollection<StringValue> NameList
        {
            get
            {
                return _nameList;
            }

            set
            {
                _nameList = value;

                _nameList.CollectionChanged += MyPropertyCollectionChanged;
            }
        }

        #endregion

        #region CTor

        public ViewModel()
        {
            var model = new Model();
            model.PropertyChanged += ModelPropertyChanged;
            NameList = model.List;
        }

        #endregion

        #region PropeetyChanged Handlers

        void ModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            NameList = (ObservableCollection<StringValue>)sender; //For Get any new entity Changed 
        }

        void MyPropertyCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            {
                NotifyPropertyChanged("NameList");
            }
        }

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        public void Execute(object sender, object parameter)
        {
            ((ICommand)sender).Execute(parameter);
        }

        #endregion
    }


}

#region I Classes

public class AddToList : ICommand
{
    #region CTor

    public AddToList(ViewModel viewModel)
    {
        ViewModel = viewModel;
    }

    #endregion

    #region Properties

    public ViewModel ViewModel { get; set; }

    #endregion

    #region ICommand Members

    public void Execute(object sender)
    {
        if (string.IsNullOrEmpty(ViewModel.Text))
            return;
        if (string.IsNullOrEmpty(ViewModel.Text.Trim()))
            return;

        ViewModel.NameList.Add(new StringValue(ViewModel.Text));
    }

    #endregion
}

public class TextChanged : ICommand
{
    #region CTor

    public TextChanged(ViewModel viewModel)
    {
        DataAccess = viewModel;
    }

    #endregion

    #region Properties

    public ViewModel DataAccess { get; set; }

    #endregion

    #region ICommand Members

    public void Execute(object sender)
    {
        DataAccess.Text = sender.ToString();
    }

    #endregion
}

#endregion



#region StringValue Class

public class StringValue
{
    public string Value { get; set; }

    public StringValue(string value)
    {
        Value = value;
    }
}

#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
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
Microsoft Certified Technology Specialist (MCTS)


"شاهین خورشیدنیا"


Nobody is perfect and neither am I, but I am trying to be more professional. In my humble opinion, you have to develop your skills, as long as you are responsible for developing even a tiny part of a sustainable software. That makes this job so special to me.

Comments and Discussions