Skip to content

Instantly share code, notes, and snippets.

@arktronic
Created September 16, 2012 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arktronic/3730852 to your computer and use it in GitHub Desktop.
Save arktronic/3730852 to your computer and use it in GitHub Desktop.
OnPropertyChanged with expressions
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);
}
@SimonCropp
Copy link

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.

public bool Something;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment