Friday, August 2, 2013

Caller Information - .NET 4.5

Pretty slick way to implement INotifyPropertyChanged, without having to pass in property names (string parameters).

http://msdn.microsoft.com/en-us/library/hh534540.aspx

public class Notifiable : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
 
public void OnPropertyChanged([CallerMemberName] string property = null)
{
//get an instance of the current handle(s)
var handler = PropertyChanged;

if (handler != null && property != null)
{
var args = new PropertyChangedEventArgs(property);
handler(this, args);
}
}
 }

Then simply call OnPropertyChanged() in your property setter:


private decimal input1;
public decimal Input1
{
get { return input1; }
set
                                {
                                                if (value != input1)
                                                {
                                                                input1 = value;
                                                                OnPropertyChanged();
                                                }
                                }
                }