问题描述
我正在使用MVVM模式与WPF并遇到一个问题,我可以简化如下:我有一个CardType模型。 p>
public class CardType
{
public int Id {get;组; }
public string Name {get;组;
}
我有一个使用CardType的viewmodel。
public class ViewModel:INotifyPropertyChanged
{
private CardType selectedCardType;
public CardType SelectedCardType
{
get {return selectedCardType; }
set
{
selectedCardType = value;
OnPropertyChanged(nameof(SelectedCardType));
}
}
public IEnumerable< CardType> CardTypes {get;组; }
// ...等等...
}
我的XAML有一个ComboBox,它的项目基于CardTypes,并且应该根据SelectedCardType预选一个项目。
ComboBox ItemsSource ={Binding CardTypes}
DisplayMemberPath =Name
SelectedItem ={Binding SelectedCardType}/>
由于我的控制之外的原因,SelectedCardType对象将是一个引用不等的副本在CardTypes中的项目。因此,WPF无法将SelectedItem与ItemsSource中的项匹配,并且当我运行该应用程序时,ComboBox最初出现时未选择任何项目。
我尝试覆盖Equals )和GetHashCode()方法,但WPF仍然无法匹配项目。
鉴于我的特殊约束,我如何让ComboBox选择正确的项? / p>
您可能不会覆盖等于
和 GetHashCode
正确。这应该适合你。 (但是,只要覆盖Equals就可以在你的情况下工作,但是当你重写等级的等级时,它被认为是一个很好的做法来覆盖GetHashCode)
public class CardType
{
public int Id {get;组; }
public string Name {get;组;
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();
}
}
I'm using the MVVM pattern with WPF and have run into a problem, which I can simplify to the following:
I have a CardType model.
public class CardType
{
public int Id { get; set; }
public string Name { get; set; }
}
And I have a viewmodel that consumes 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.
<ComboBox ItemsSource="{Binding CardTypes}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedCardType}"/>
For reasons outside of my control, the SelectedCardType object will be a reference-unequal copy of 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.
I tried overriding the Equals() and GetHashCode() methods on CardType, but WPF still fails to match the items.
Given my peculiar constraints, how can I get ComboBox to select the correct item?
You might not be overriding Equals
and GetHashCode
properly. 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)
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();
}
}
这篇关于如何将ComboBox的SelectedItem绑定到ItemSource中的项目副本的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!