namespace Caliburn.Micro.Core {
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.Serialization;
///
/// A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling.
///
[DataContract]
public class PropertyChangedBase : INotifyPropertyChangedEx {
///
/// Creates an instance of .
///
public PropertyChangedBase() {
IsNotifying = true;
}
///
/// Occurs when a property value changes.
///
public virtual event PropertyChangedEventHandler PropertyChanged;
///
/// Enables/Disables property change notification.
/// Virtualized in order to help with document oriented view models.
///
public virtual bool IsNotifying { get; set; }
///
/// Raises a change notification indicating that all bindings should be refreshed.
///
public virtual void Refresh() {
NotifyOfPropertyChange(string.Empty);
}
///
/// Notifies subscribers of the property change.
///
/// Name of the property.
#if NET || SILVERLIGHT
public virtual void NotifyOfPropertyChange(string propertyName) {
#else
public virtual void NotifyOfPropertyChange([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
#endif
if (IsNotifying && PropertyChanged != null) {
Execute.OnUIThread(() => OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
}
}
///
/// Notifies subscribers of the property change.
///
/// The type of the property.
/// The property expression.
public void NotifyOfPropertyChange(Expression> property) {
NotifyOfPropertyChange(property.GetMemberInfo().Name);
}
///
/// Raises the event directly.
///
/// The instance containing the event data.
[EditorBrowsable(EditorBrowsableState.Never)]
protected void OnPropertyChanged(PropertyChangedEventArgs e) {
var handler = PropertyChanged;
if (handler != null) {
handler(this, e);
}
}
///
/// Raises the property changed event immediately.
///
/// Name of the property.
public virtual void RaisePropertyChangedEventImmediately(string propertyName)
{
if (IsNotifying)
RaisePropertyChangedEventCore(propertyName);
}
void RaisePropertyChangedEventCore(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}