по первой ссылке
| Цитата | | BookViewModel унаследован от класса ViewModelBase, который заботливо сгенерил нам MVVM Toolkit. ViewModelBase же, в свою очередь, реализует интерфейс INotifyPropertyChanged и содержит функцию OnPropertyChanged. Все это нужно для того, чтобы всегда можно было вызвать событие "изменилось такое-то поле". |
пример реализации
| Код | /// <summary> /// Base class for all ViewModel classes in the application. It provides support for property change notifications /// and has a DisplayName property. This class is abstract. /// </summary> public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable { private readonly Dispatcher _dispatcher;
/// <summary> /// Returns the dispatcher object to perform operations on UI thread. /// </summary> protected Dispatcher Dispatcher { get { return _dispatcher; } }
/// <summary> /// Returns the user-friendly name of this object. Child classes can set this property to a new value, /// or override it to determine the value on-demand. /// </summary> public virtual string DisplayName { get; protected set; }
protected ViewModelBase() { if (System.Windows.Application.Current != null) { _dispatcher = System.Windows.Application.Current.Dispatcher; } else { //this is useful for unit tests where there is no application running _dispatcher = Dispatcher.CurrentDispatcher; } } #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) { VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { var e = new PropertyChangedEventArgs(propertyName); handler(this, e); } }
#endregion // INotifyPropertyChanged Members
#region IDisposable Members
/// <summary> /// Invoked when this object is being removed from the application /// and will be subject to garbage collection. /// </summary> public void Dispose() { OnDispose(); }
/// <summary> /// Child classes can override this method to perform /// clean-up logic, such as removing event handlers. /// </summary> protected virtual void OnDispose() { }
#endregion // IDisposable Members } |
|