WPF Tips:ViewModelBase

对于View model,我们都需要实现 INotifyPropertyChanged 接口。所以,我写了下面这两个很小但是很实用的类:


ObservableObject

namespace Infrastructure
{
    using System.ComponentModel;

    /// 
    /// Base class for observable object with property Changed notification.
    /// 
    public abstract class ObservableObject : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        /// 
        /// Occurs when a property value changes.
        /// 
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Protected Methods
        /// 
        /// Raises the PropertyChanged event.
        /// 
        /// The name of the changed property.
        protected void RaisePropertyChangedEvent(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
                this.PropertyChanged(this, e);
            }
        }
        #endregion
    }
}

ViewModelBase

namespace Infrastructure
{
    /// 
    /// Base class for view model.
    /// 
    public abstract class ViewModelBase : ObservableObject
    {
    }
}


你可能感兴趣的:(C#,WPF,.Net)