tags:

views:

452

answers:

3

What is the purpose of INotifyPropertyChanged. I know this event is fired whenever a property is changed but how can the View/UI knows that this event is fired:

Here is my Customer class that implements the INotifyPropertyChanged event:

public class Customer : INotifyPropertyChanged
    {
        private string _firstName;

        public string LastName { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if(PropertyChanged != null)
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));

        }

        public string FirstName
        {
            get { return _firstName; }

            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
    }

But now how to notify the UI that property has changed. Like when the user assigns null or empty to the first name how can I display a MessageBox on the UI.

+2  A: 

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.

flq
I know about updating the UI due to INotifyPropertyChanged interface but how can I change something on the UI based on the fact that FirstName is null or empty.
If you really do not want to attach yourself to the event and do what you want to do in imperative fashion, you may be able to do it declaratively by a combination of e.g. a Converter for string -> bool encapsulating your logic and then drive some Visibility property with the resulting bool. However, I don't see how to pop up a MessageBox from within XAML.
flq
+1  A: 

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
        }
    }
}
sixlettervariables
A: 

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.

Reed Copsey