Skip to content

AccessBeforeValue

Simon Cropp edited this page Jul 30, 2012 · 2 revisions

Sometimes it is useful to have access to the value of the property be changed from OnPropertyChanging.

public void OnPropertyChanging(string propertyName, object before)

So for example if your code looks like this

public class Person : INotifyPropertyChanging
{
    public event PropertyChangingEventHandler PropertyChanging;

    public string Name { get; set; }

    public void OnPropertyChanging(string propertyName, object before)
    {
        var propertyChanging = PropertyChanging;
        if (propertyChanging != null)
        {
            propertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }
}

The following will be compiled

public class Person : INotifyPropertyChanging
{
    private string name;

    public event PropertyChangingEventHandler PropertyChanging;

    public string Name
    {
        get { return name; }
        set
        {
            OnPropertyChanging("Name", Name);
            name = value;
        }
    }

    public void OnPropertyChanging(string propertyName, object before)
    {
        var propertyChanging = PropertyChanging;
        if (propertyChanging != null)
        {
            propertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

}
Clone this wiki locally