假设我定义了一个类,如下所示。现在让我们说我进一步修改这个类以正确实现 NotifyPropertyChanged 并添加到我绑定到 WPF 网格的集合中。如果执行代码更改其中一个实例的 Name 或 Description 属性,其他实例也会更新吗?WPF/XAML 绑定是否将这些相同的对象视为相同的对象,还是将实例视为不同的并且仅更新已更改对象的属性?
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;
}
}
}