Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NotifyOfProperty change from inside of ObservableCollection

How can I trigger NotifyOfPropertyChange by changing property of item inside of ObservableCollection?

Currently I am assigning null to ObservableCollection and then the original value. However, I don’t think this is a proper solution.

ObservableCollection<Item> temp = Collection;
Collection = null;
Collection = temp;

I have also tried to implement INotifyPropertyChanged interface, but it didn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Item : INotifyPropertyChanged
{
    public string Name { get; set; }

    public event PropertyChangedEventHandler? PropertyChanged;
}

>Solution :

Implementing INotifyPropertyChanged is not enough, you actually have to use it.

See INotifyPropertyChanged Interface documentation for an example.

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    // This method is called by the Set accessor of each property.  
    // The CallerMemberName attribute that is applied to the optional propertyName  
    // parameter causes the property name of the caller to be substituted as an argument.  
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


    private string m_name = "";

    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            if (m_name != value)
            {
                m_name = value;
                NotifyPropertyChanged();
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading