Source

main-vinayFork / Source / Excel / Excel.AddIn / Excel.AddIn / ViewModels / ViewModelBase.cs

Full commit

using System;
using System.ComponentModel;
using DataUp.Excel.Common;

namespace DataUp.Excel.Addin
{
    /// <summary>
    /// This is the base class to be used by all viewModels that want the 
    /// property change notifications to be raised for the binding system.
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        private bool isBusy;

        /// <summary>
        /// Gets or sets value indicating whether the system is busy processing data.
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return isBusy;
            }
            set
            {
                isBusy = value;
                OnNotifyPropertyChanged("IsBusy");
            }
        }

        public event EventHandler RunWorkerStarted;

        public event EventHandler<RunWorkerCompletedEventArgs> RunWorkerCompleted;

        /// <summary>
        /// This even is handled by the binding system to refresh the data when the value is changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaiseRunWorkerStarted(object sender)
        {
            this.RunWorkerStarted.OnFire(sender, new EventArgs());
        }

        public void RaiseRunWorkerStarted(object sender, EventArgs e)
        {
            this.RunWorkerStarted.OnFire(sender, e);
        }

        public void RaiseRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.RunWorkerCompleted.OnFire<RunWorkerCompletedEventArgs>(sender, e);
        }

        /// <summary>
        /// This is the method that should be called by derived classes to raise the property change event notification 
        /// so that the binding systems refreshes the data on the UI.
        /// </summary>
        /// <param name="propertyName">propertyName that is changed / updated</param>
        protected void OnNotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                PropertyChangedEventArgs arguments = new PropertyChangedEventArgs(propertyName);
                handler(this, arguments);
            }
        }
    }
}