【问题标题】:How do you bind a ComboBox's SelectedItem to an object that is a copy of an item from ItemsSource?如何将 ComboBox 的 SelectedItem 绑定到作为 ItemsSource 中项目副本的对象?
【发布时间】:2016-03-12 11:03:26
【问题描述】:

我在 WPF 中使用 MVVM 模式并遇到了问题,我可以将其简化为以下内容:

我有一个 CardType 模型。

public class CardType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

我有一个使用 CardType 的视图模型。

public class ViewModel : INotifyPropertyChanged
{
    private CardType selectedCardType;
    public CardType SelectedCardType
    {
        get { return selectedCardType; }
        set
        {
            selectedCardType = value;
            OnPropertyChanged(nameof(SelectedCardType));
        }
    }

    public IEnumerable<CardType> CardTypes { get; set; } 

    // ... and so on ...
}

我的 XAML 有一个 ComboBox,它的项目基于 CardTypes,并且应该根据 SelectedCardType 预先选择一个项目。

<ComboBox ItemsSource="{Binding CardTypes}"
          DisplayMemberPath="Name"
          SelectedItem="{Binding SelectedCardType}"/>

由于我无法控制的原因,SelectedCardType 对象将是 CardTypes 中项目的引用不相等副本。因此 WPF 无法将 SelectedItem 与 ItemsSource 中的项目匹配,当我运行应用程序时,ComboBox 最初出现时没有选择任何项目。

我尝试覆盖 CardType 上的 Equals() 和 GetHashCode() 方法,但 WPF 仍然无法匹配项目。

鉴于我的特殊限制,我怎样才能让 ComboBox 选择正确的项目?

【问题讨论】:

标签: wpf xaml mvvm data-binding combobox


【解决方案1】:

您可能没有正确覆盖 EqualsGetHashCode。这应该适合你。 (但是,仅覆盖 Equals 将适用于您的情况,但是当您为类覆盖 Equals 时,覆盖 GetHashCode 也被认为是一种好习惯)

public class CardType
{
    public int Id { get; set; }
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        CardType cardType = obj as CardType;
        return cardType.Id == Id && cardType.Name == Name;
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode() & Name.GetHashCode();
    }
}

【讨论】:

  • 我很快在小样本中试用了它,对我来说效果很好。你能添加你如何从代码中设置 SelectedCardType 的代码吗?
  • Id.GetHashCode() 可以只替换为 Id
【解决方案2】:

您可以使用 SelectedValue 和 SelectedValuePath:

<ComboBox ItemsSource="{Binding CardTypes}"
          DisplayMemberPath="Name"
          SelectedValue="{Binding ProductId, Mode=TwoWay}" 
          SelectedValuePath="Id"/>

其中 ProductId 是带有 NotifyPropertyChanged 的​​ int 属性。

在这里阅读一个很好的解释: Difference between SelectedItem, SelectedValue and SelectedValuePath

【讨论】:

  • 这需要我将视图模型的 SelectedCardType 属性更改为整数,我不能随意这样做。
  • 好的,用这个方法肯定你至少要加一个整数。
【解决方案3】:

您可以做的解决方法是将您的 SelectedItem 绑定到一个字符串(而不是 cardType),然后使用该字符串创建一个 CardType 类型的对象?

【讨论】:

    猜你喜欢
    • 2014-12-10
    • 2015-09-29
    • 2019-12-11
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多