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
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();
}
}
}
}