/ViewModel/ViewModelBase.cs
C# | 116 lines | 73 code | 17 blank | 26 comment | 9 complexity | 74255e204bdfca6c071ed881be052049 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Windows.Input;
-
- namespace QuickArch.ViewModel
- {
- public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
- {
- RelayCommand _closeCommand;
-
- public virtual string DisplayName { get; set; }
-
- #region Debugging Aides
- /// <summary>
- /// Warns the developer if this object does not have
- /// a public property with the specified name. This
- /// method does not exist in a Release build.
- /// </summary>
- [Conditional("DEBUG")]
- [DebuggerStepThrough]
- public void VerifyPropertyName(string propertyName)
- {
- // Verify that the property name matches a real,
- // public, instance property on this object.
- if (TypeDescriptor.GetProperties(this)[propertyName] == null)
- {
- string msg = "Invalid property name: " + propertyName;
-
- if (this.ThrowOnInvalidPropertyName)
- throw new Exception(msg);
- else
- Debug.Fail(msg);
- }
- }
-
- /// <summary>
- /// Returns whether an exception is thrown, or if a Debug.Fail() is used
- /// when an invalid property name is passed to the VerifyPropertyName method.
- /// The default value is false, but subclasses used by unit tests might
- /// override this property's getter to return true.
- /// </summary>
- protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
-
- #endregion // Debugging Aides
-
- #region CloseCommand
- //returns the command that attempts to remove this workspace from the UI
- public ICommand CloseCommand
- {
- get
- {
- if (_closeCommand == null)
- _closeCommand = new RelayCommand(param => this.OnRequestClose());
-
- return _closeCommand;
- }
- }
- #endregion
-
- #region RequestClose [event]
- //raised when this workspace should be removed from the UI
- public event EventHandler RequestClose;
-
- void OnRequestClose()
- {
- EventHandler handler = this.RequestClose;
- if (handler != null)
- handler(this, EventArgs.Empty);
- }
- #endregion
-
- #region INotifyPropertyChanged Members
-
- /// <summary>
- /// Raised when a property on this object has a new value.
- /// </summary>
- public event PropertyChangedEventHandler PropertyChanged;
-
- /// <summary>
- /// Raises this object's PropertyChanged event.
- /// </summary>
- /// <param name="propertyName">The property that has a new value.</param>
- protected virtual void OnPropertyChanged(string propertyName)
- {
- this.VerifyPropertyName(propertyName);
-
- PropertyChangedEventHandler handler = this.PropertyChanged;
- if (handler != null)
- {
- var e = new PropertyChangedEventArgs(propertyName);
- handler(this, e);
- }
- }
-
- #endregion // INotifyPropertyChanged Members
-
- #region IDisposable Members
- //invoked when this objected is being removed from the application and will
- //be subject to garbage collection
- public void Dispose()
- {
- this.OnDispose();
- }
-
- //Child classes can override this method to perform
- //clean up logic, such as removing event handlers
- protected virtual void OnDispose()
- {
- }
- #endregion
- }
- }