8

当我们将组合框的 SelectedItem 绑定到一个属性时,通常它会使用对象类型的 Equals 方法来确定应该在 ComboBox 视图中显示的选定项。(例如,请参阅此问题

是否可以为此拥有自己的比较器,而无需修改类的 equals 方法?我不想直接修改方法的原因是因为该类也用于业务逻辑,我不希望我的相等比较器影响使用同一类的其他东西

4

1 回答 1

1

The reason I don't want to modify the method directly is because the class is also used for business logic and I don't want my equality comparer to affect other things that use the same class

This usually indicates a wrapper that is needed:

public class Wrapper<T>
{
    public override string ToString() { ... }

    public override bool Equals(object obj) { ... }

    public T UnderlyingRecord { get; set; }
}

This way you can carry on as normal and only the view will use the wrapper object.

于 2013-07-22T18:55:14.920 回答