wpf 如何将 ComboBox 的 SelectedItem 绑定到作为 ItemsSource 项目副本的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34140431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you bind a ComboBox's SelectedItem to an object that is a copy of an item from ItemsSource?
提问by Bryan
I'm using the MVVM pattern with WPF and have run into a problem, which I can simplify to the following:
我在 WPF 中使用 MVVM 模式并遇到了一个问题,我可以将其简化为以下内容:
I have a CardType model.
我有一个 CardType 模型。
public class CardType
{
public int Id { get; set; }
public string Name { get; set; }
}
And I have a viewmodel that consumes CardType.
我有一个使用 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 ...
}
My XAML has a ComboBox that bases its items on CardTypes and should preselect an item based on SelectedCardType.
我的 XAML 有一个 ComboBox,它的项目基于 CardType,并且应该基于 SelectedCardType 预选一个项目。
<ComboBox ItemsSource="{Binding CardTypes}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedCardType}"/>
For reasons outside of my control, the SelectedCardType object will be a reference-unequal copyof the item in CardTypes. Therefore WPF fails to match the SelectedItem to an item in ItemsSource, and when I run the app, the ComboBox initially appears with no item selected.
由于我无法控制的原因,SelectedCardType 对象将是CardTypes 中项目的引用不相等副本。因此,WPF 无法将 SelectedItem 与 ItemsSource 中的项目匹配,并且当我运行该应用程序时,ComboBox 最初出现时未选择任何项目。
I tried overriding the Equals() and GetHashCode() methods on CardType, but WPF still fails to match the items.
我尝试覆盖 CardType 上的 Equals() 和 GetHashCode() 方法,但 WPF 仍然无法匹配这些项目。
Given my peculiar constraints, how can I get ComboBox to select the correct item?
鉴于我的特殊限制,我怎样才能让 ComboBox 选择正确的项目?
回答by Rohit Vats
You might not be overriding Equalsand GetHashCodeproperly. This should work for you. (However, just overriding Equals will work in your case but it's considered to be good practice to override GetHashCode too when you override Equals for a class)
你可能不被覆盖Equals并GetHashCode正确。这应该对你有用。(但是,仅覆盖 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();
}
}
回答by Giangregorio
You can use SelectedValue and SelectedValuePath:
您可以使用 SelectedValue 和 SelectedValuePath:
<ComboBox ItemsSource="{Binding CardTypes}"
DisplayMemberPath="Name"
SelectedValue="{Binding ProductId, Mode=TwoWay}"
SelectedValuePath="Id"/>
Where ProductId is a int property with NotifyPropertyChanged.
其中 ProductId 是一个带有 NotifyPropertyChanged 的 int 属性。
Read a great explanation here: Difference between SelectedItem, SelectedValue and SelectedValuePath
在这里阅读一个很好的解释: SelectedItem、SelectedValue 和 SelectedValuePath 之间的区别
回答by Eamon Scullion
A workaround that you could do is to bind your SelectedItem to a string (instead of cardType), then create an object of type CardType using this string?
您可以做的解决方法是将 SelectedItem 绑定到字符串(而不是 cardType),然后使用此字符串创建 CardType 类型的对象?

