INotifyPropertyChanged WPF
Asked Answered
F

4

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.

Fanlight answered 24/6, 2009 at 16:37 Comment(0)
G
5

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.

Gileadite answered 24/6, 2009 at 16:42 Comment(0)
G
3

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.

Glutinous answered 24/6, 2009 at 16:41 Comment(2)
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.Fanlight
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.Glutinous
L
3

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
        }
    }
}
Loud answered 24/6, 2009 at 16:42 Comment(0)
C
0

An INotifyPropertyChabged object allows you to notify observers that a property of that object has changed and act accordingly using the Subscribe(...) method.

Extracted from https://github.com/sergio235/Venn documentation:

var observable = myObject.WhenAny(obj => obj.MyProperty);
observable.Subscribe(newValue => {
     // Handle property change
});
Clermontferrand answered 27/12, 2023 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.