lets say i have a class defined as shown below. Lets now say i further modify this class to implement NotifyPropertyChanged correctly and add to a collection
which i bind to WPF grid. If code executes that changes Name or Description property of one of these instances, will the others get updated as well?
Does WPF/XAML binding consider these the same object or does it treat the instances as different and only updates the changed object’s property?
class Security
{
public string Ticker { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public override bool Equals(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
Security security = (Security)obj;
//reference check
//if (!Object.Equals(Ticker, security.Ticker)) return false;
//value member check
if (!Ticker.Equals(security.Ticker)) return false;
return true;
}
public static bool operator ==(Security sec1, Security sec2)
{
if (System.Object.ReferenceEquals(sec1, sec2)) return true;
if (((object)sec1 == null) || ((object)sec2 == null)) return false;
// Return true if the fields match:
return sec1.Ticker == sec2.Ticker;
}
public static bool operator !=(Security sec1, Security sec2)
{
return !(sec1 == sec2);
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + Ticker.GetHashCode();
hash = hash * 23 + Ticker.GetHashCode();
hash = hash * 23 + Ticker.GetHashCode();
return hash;
}
}
}
A fairly loose description of how binding works is that the xaml class builds a lookup table of properties that it’s bound to. When the PropertyChanged event is raised the xaml class checks it’s lookup table for a property matching the argument and refreshes the value. Therefore, only the value of the property whose name is passed in the PropertyChanged event will be updated.
An implementation would like like the following:
See the documentation on INotifyPropertyChanged here and here for more information.