gpt4 book ai didi

.net - WPF ComboBox 的自定义相等比较器

转载 作者:行者123 更新时间:2023-12-04 12:55:21 24 4
gpt4 key购买 nike

当我们将组合框的 SelectedItem 绑定(bind)到一个属性时,通常它会使用对象类型的 Equals 方法来确定应该在 ComboBox View 中显示的选定项。 (例如,参见 this question)

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

最佳答案

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



这通常表示需要一个包装器:
public class Wrapper<T>
{
public override string ToString() { ... }

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

public T UnderlyingRecord { get; set; }
}

这样您就可以正常进行,并且只有 View 会使用包装器对象。

关于.net - WPF ComboBox 的自定义相等比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13522282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com