INotifyPropertyChanged WPF

INotifyPropertyChanged allows WPF UI elements (via the standard data binding mechanisms) to subscribe to the PropertyChanged event and automatically update themselves. For example, if you had a TextBlock displaying your FirstName property, by using INotifyPropertyChanged, you can display it on a form, and it will automatically stay up to date when the FirstName property is changed in code.

The View just subscribes to the event - which tells it everything necessary. The event includes the name of the changed property, so if a UI element is bound to that property, it updates.


WPF can know because it can inspect whether an object implements this interface, then cast the object to said interface and register for the event. It can then trigger the Binding Infrastructure to update the display. If you want to react as well, you can register yourself for the same event.


EDIT: I reread your question and some of your comments. Here is a possible solution utilizing the DataContextChanged event and the INotifyPropertyChanged interface on your Customer object. You should also look into Data Binding Validation in WPF and .Net 3.5.

<TextBox Text="{Binding FirstName}" />

// assuming:
// myWindow.DataContext = new Customer();
myWindow.DataContextChanged += MyWindow_DataContextChanged;

private void MyWindow_DataContextChanged(object sender,
    DependencyPropertyChangedEventArgs e)
{
    var oldCustomer = e.OldValue as Customer;
    if (oldCustomer != null)
    {
        oldCustomer.PropertyChanged -= Customer_CheckProps;
    }

    var newCustomer = e.NewValue as Customer;
    if (newCustomer != null)
    {
        newCustomer.PropertyChanged += Customer_CheckProps;
    }
}

private void Customer_CheckProps(object sender, PropertyChangedEventArgs e)
{
    var customer = sender as Customer;
    if (customer != null)
    {
        if (e.PropertyName == "FirstName"
            && String.IsNullOrEmpty(customer.FirstName))
        {
            // Display Message Box
        }
    }
}