Add docs to PropertyChangedBase, and create OnPropertyChanged method to integrate with Fody.PropertyChanged

This commit is contained in:
Antony Male 2014-03-20 08:10:37 +00:00
parent 5c8d3b518a
commit ed4f6233f1
1 changed files with 34 additions and 3 deletions

View File

@ -12,7 +12,10 @@ namespace Stylet
public class PropertyChangedBase : INotifyPropertyChanged, INotifyPropertyChangedDispatcher public class PropertyChangedBase : INotifyPropertyChanged, INotifyPropertyChangedDispatcher
{ {
private Action<Action> _propertyChangedDispatcher = Execute.DefaultPropertyChangedDispatcher; private Action<Action> _propertyChangedDispatcher = Execute.DefaultPropertyChangedDispatcher;
public Action<Action> PropertyChangedDispatcher /// <summary>
/// Dispatcher to use to dispatch PropertyChanged events. Defaults to Execute.DefaultPropertyChangedDispatcher
/// </summary>
public virtual Action<Action> PropertyChangedDispatcher
{ {
get { return this._propertyChangedDispatcher; } get { return this._propertyChangedDispatcher; }
set { this._propertyChangedDispatcher = value; } set { this._propertyChangedDispatcher = value; }
@ -20,17 +23,39 @@ namespace Stylet
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Refresh all properties
/// </summary>
public void Refresh() public void Refresh()
{ {
this.NotifyOfPropertyChange(String.Empty); this.NotifyOfPropertyChange(String.Empty);
} }
/// <summary>
/// Raise a PropertyChanged notification from the property in the given expression, e.g. NotifyOfPropertyChange(() => this.Property)
/// </summary
/// <param name="property">Expression describing the property to raise a PropertyChanged notification for</param>
protected void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property) protected void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
{ {
this.NotifyOfPropertyChange(property.NameForProperty()); this.OnPropertyChanged(property.NameForProperty());
} }
/// <summary>
/// Raise a PropertyChanged notification from the property with the given name
/// </summary>
/// <param name="propertyName">Name of the property to raise a PropertyChanged notification for. Defaults to the calling property</param>
protected virtual void NotifyOfPropertyChange([CallerMemberName] string propertyName = "") protected virtual void NotifyOfPropertyChange([CallerMemberName] string propertyName = "")
{
this.OnPropertyChanged(propertyName);
}
/// <summary>
/// Fires the PropertyChanged notification.
/// </summary>
/// <remarks>Specially named so that Fody.PropertyChanged calls it</remarks>
/// <param name="propertyName">Name of the property to raise the notification for</param>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual void OnPropertyChanged(string propertyName)
{ {
var handler = this.PropertyChanged; var handler = this.PropertyChanged;
if (handler != null) if (handler != null)
@ -39,9 +64,15 @@ namespace Stylet
} }
} }
/// <summary>
/// Takes, by reference, a field, and its new value. If field != value, will set field = value and raise a PropertyChanged notification
/// </summary>
/// <param name="field">Field to assign</param>
/// <param name="value">Value to assign to the field, if it differs</param>
/// <param name="propertyName">Name of the property to notify for. Defaults to the calling property</param>
protected virtual void SetAndNotify<T>(ref T field, T value, [CallerMemberName] string propertyName = "") protected virtual void SetAndNotify<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
{ {
if (Comparer<T>.Default.Compare(field, value) != 0) if (!EqualityComparer<T>.Default.Equals(field, value))
{ {
field = value; field = value;
this.NotifyOfPropertyChange(propertyName); this.NotifyOfPropertyChange(propertyName);