Created
December 17, 2010 14:21
-
-
Save renestein/745001 to your computer and use it in GitHub Desktop.
Třída PropertyNotificationBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PropertyNotificationBase : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged = delegate{}; | |
public PropertyNotificationBase() | |
{ | |
SuppressPropertyChangedNotification = false; | |
} | |
public SynchronizationContext SynchContext | |
{ | |
get; | |
set; | |
} | |
public bool SuppressPropertyChangedNotification | |
{ | |
get; | |
set; | |
} | |
public virtual void RaisePropertyChangedEvent(string propertyName) | |
{ | |
var eargs = new PropertyChangedEventArgs(propertyName); | |
OnPropertyChanged(eargs); | |
} | |
public virtual void RaisePropertyChangedEvent<TReturn>(Expression<Func<TReturn>> propertyDefinition) | |
{ | |
if (propertyDefinition == null) | |
{ | |
throw new ArgumentNullException("propertyDefinition"); | |
} | |
var memberExpression = propertyDefinition.Body as MemberExpression; | |
if (memberExpression == null) | |
{ | |
throw new ArgumentException("propertyDefinition"); | |
} | |
RaisePropertyChangedEvent(memberExpression.Member.Name); | |
} | |
public virtual void RaiseAllPropertiesChanged() | |
{ | |
GetType() | |
.GetProperties(BindingFlags.Public | BindingFlags.Instance) | |
.ToList() | |
.ForEach(property => RaisePropertyChangedEvent(property.Name)); | |
} | |
public virtual void RaisePropertiesChanged(params string[] properties) | |
{ | |
properties.ToList().ForEach(property => RaisePropertyChangedEvent(property)); | |
} | |
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) | |
{ | |
if (SuppressPropertyChangedNotification) | |
{ | |
return; | |
} | |
var PropertyChangedHandler = PropertyChanged; | |
if (SynchContext != null) | |
{ | |
SynchContext.Send(state => PropertyChangedHandler(this, e), null); | |
} | |
else | |
{ | |
PropertyChangedHandler(this, e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment