は、次のようにクラスを定義していると言います。ここでは、このクラスをさらに変更してNotifyPropertyChangedを正しく実装し、WPFグリッドにバインドする コレクションに追加するとしましょう。これらのインスタンスのいずれかのNameまたはDescriptionプロパティを変更するコードを実行すると、他のインスタンスも更新されますか? WPF/XAMLバインディングはこれらのオブジェクトを同じオブジェクトとみなしますか、またはインスタンスを異なるものとして扱い、変更されたオブジェクトのプロパティのみを更新しますか?モデルのEqualとHashcodeをバインドして上書きする
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;
}
}
}
thanks..this私は知っていると思います。私は質問をはっきりと説明していないかもしれません。説明するのが少し難しい...私はequalsとgethashcodeの等価性は参照に基づいていないが、プロパティの内容をオーバーライドと言うことができます。私のクラスの2つのインスタンスが2つのテキストボックスにバインドされているが、新たに定義された等価性の点では、実際には等しくなります。 – mike01010
@ mike01010私はそうは思わない。私はバインディングがどのように正確に実装されているのかわかりませんが、でキーイングされていて、PropertyChangedを呼び出すと参照テーブルがリフレッシュされ、クラスのインスタンスが1つしかないことが推測されます。 –