Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the concept behind INotifyPropertyChanged?

Tags:

silverlight

All the examples of Silverlight using MVVM use interface named IPropertyChanged. What is the concept behind it and why do we need to raise an event whenever we set some value?

Eg:-

public class UserNPC:INotifyPropertyChanged
{
    private string name;
    public string Name { 
        get { return name; } 
        set { name = value; onPropertyChanged(this, "Name"); } 
    }
    public int grade;
    public int Grade { 
        get { return grade; } 
        set { grade = value; onPropertyChanged(this, "Grade"); } 
    }

    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;

    // OnPropertyChanged will raise the PropertyChanged event passing the
    // source property that is being updated.
    private void onPropertyChanged(object sender, string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
        }
    }
}

What is the exact purpose of INotifyPropertyChanged?

like image 721
TCM Avatar asked Aug 24 '10 15:08

TCM


People also ask

How do you implement INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

Why PropertyChanged is null?

Although you haven't shown us the XAML use of the property I would think that one way the PropertyChanged is null is when you set the value of a control (which is bound) in code, this will destroy any binding.

Does ObservableCollection implement INotifyPropertyChanged?

ObservableCollection implements INotifyCollectionChanged which will notify us when the collection has a mutated state. This means creates, adds, updates, deletes. Very few of us are explained this difference when we come to MVVM.

What is OnPropertyChanged C#?

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated. Let's take an example for understanding this interface. Take one WPF Window in which there are a total of three fields: First Name, Last Name and Full Name.


1 Answers

You have the following dependencies:

View → Binding → Model

Now, the concept is as following:

If some data in your Model object changes, you are required to raise the PropertyChanged event. Why? Because the Binding object has registered a method with the data object's PropertyChanged event.

So all you have to do when something changes within your Model object is to raise the event and you are done.

When you do that, the Binding object gets notified about the change through your event. The Binding object in turn lets the View object know that something happened. The View object then can update the UI if necessary.

Code example

Here you have a compilable example. Set a few breakpoints, step through the code with F11 and see what happens behind the scenes. Note that this example has the following dependency: View → Model. I left out the Binding object.

using System;
using System.ComponentModel;

namespace INotifyPropertyChangedDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create 2 listeners.
            View1 view1 = new View1();
            View2 view2 = new View2();

            // Create 1 data object.
            Model model = new Model();

            // Connect listener with data object.
            model.PropertyChanged += new PropertyChangedEventHandler(view1.MyPropertyChangedEventHandler);
            model.PropertyChanged += new PropertyChangedEventHandler(view2.MyPropertyChangedEventHandler);

            // Let data object publish change notification.
            model.FirstName = "new name";

            // Check whether all listeners got notified.
            // ... via console.
        }

        public class Model : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            private string firstName;
            public string FirstName
            {
                get { return firstName; }
                set
                {
                    if (firstName != value)
                    {
                        firstName = value;
                        if (PropertyChanged != null)
                        {
                            PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                        }
                    }
                }
            }
        }

        public class View1
        {
            public void MyPropertyChangedEventHandler(object source, PropertyChangedEventArgs arg)
            {
                Console.WriteLine("Listener 1: Changed Property: {0}", arg.PropertyName);
                string newValue = ((Model) source).FirstName;
                Console.WriteLine("Listener 1: Changed Property Value: {0}", newValue);
            }
        }

        public class View2
        {
            public void MyPropertyChangedEventHandler(object source, PropertyChangedEventArgs arg)
            {
                Console.WriteLine("Listener 2: Changed Property: {0}", arg.PropertyName);
                string newValue = ((Model)source).FirstName;
                Console.WriteLine("Listener 2: Changed Property Value: {0}", newValue);
            }
        }
    }
}
like image 123
Lernkurve Avatar answered Oct 31 '22 09:10

Lernkurve