Created
September 16, 2012 02:55
-
-
Save arktronic/3730852 to your computer and use it in GitHub Desktop.
OnPropertyChanged with expressions
This file contains 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 bool Something | |
{ | |
get | |
{ | |
return _something; | |
} | |
set | |
{ | |
if(value == _something) return; | |
_something = value; | |
OnPropertyChanged(() => Something); | |
} | |
} | |
// ------------------------------------------------------------- | |
protected virtual void OnPropertyChanged<T>(System.Linq.Expressions.Expression<Func<T>> expr) | |
{ | |
// Ensure this executes in the correct thread. | |
if(!Deployment.Current.Dispatcher.CheckAccess()) | |
{ | |
Deployment.Current.Dispatcher.BeginInvoke(() => OnPropertyChanged(expr)); | |
return; | |
} | |
var propertyName = ((System.Linq.Expressions.MemberExpression) expr.Body).Member.Name; | |
var handler = this.PropertyChanged; | |
if (handler == null) return; | |
var e = new PropertyChangedEventArgs(propertyName); | |
handler.Invoke(this, e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With a combination of Fielder (https://github.com/SimonCropp/Fielder) and PropertyChanged (https://github.com/SimonCropp/PropertyChanged) you can bind to a public field like this.