我正在创建一个显示HomeTeam
和AwayTeam
的篮球WPF应用。
我创建了一个Player
对象,在主窗口中我为主页(ObservableCollection
)和客队(HomePlayersList
)创建了AwayPlayersList
个玩家对象。我在播放器对象上使用了INotifyPropertyChanged
接口,因此当IsInGame
bool为true时,播放器会根据计数添加到两个ObservableCollection<Player>
中的一个。 (如果列表一ObservableCollection<Player> HomeTeam
或ObservableCollection<Player> AwayTeam
计数为5,则其余列表会添加到替换列表ObservableCollection<Team> HomeSub
或ObservableCollection<Team> AwaySub
。)
但是我试图区分玩家是在主场还是在客队中,并且根据玩家所在的列表,玩家将被添加到新的主页或离开的列表中。
public static ObservableCollection<Player> HomePlayersList;
public static ObservableCollection<Player> AwayPlayersList;
public static ObservableCollection<Player> HomeTeam = new ObservableCollection<Player>();
public static ObservableCollection<Player> AwayTeam = new ObservableCollection<Player>();
public static ObservableCollection<Player> HomeSub = new ObservableCollection<Player>();
public static ObservableCollection<Player> AwaySub = new ObservableCollection<Player>();
public static int HomeSubCount = 7;
public class Player: INotifyPropertyChanged
{
public static bool IsHome = true;
private static int TotalSelected = 1;
public string Id { get; set; } //player ID
public string FirstName { get; set; } //player first name
public string SecondName { get; set; } //player second name
public string KnownName { get; set; } //player known name
public string Position { get; set; } //player position
public string Number { get; set; } //player number
public bool isInGame;
public bool IsInGame
{
get { return isInGame; }
set
{
if (value != isInGame)
{
isInGame = value;
if (isInGame)
{
OnPropertyChanged("IsInGame", true);
}
else
{
OnPropertyChanged("IsInGame", false);
}
}
}
}
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
protected void OnPropertyChanged(string propertyName, bool state)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
if (state)
{
if (TotalSelected > 5 + MainWindow.HomeSubCount)
{
this.IsInGame = false;
return;
}
if (MainWindow.HomeTeam.Count < 5)
MainWindow.HomeTeam.Add(this);
else
{
if (MainWindow.HomeSub.Count < MainWindow.HomeSubCount)
{
MainWindow.HomeSub.Add(this);
}
}
TotalSelected++;
}
else
{
if (SearchForMe(MainWindow.HomeTeam) != null)
{
MainWindow.HomeTeam.Remove(SearchForMe(MainWindow.HomeTeam));
TotalSelected--;
return;
}
if (SearchForMe(MainWindow.HomeSub) != null)
{
MainWindow.HomeSub.Remove(SearchForMe(MainWindow.HomeSub));
TotalSelected--;
return;
}
}
}
private Team SearchForMe(ObservableCollection<Team> OCT)
{
return OCT.Where(i => i.Number == this.Number).SingleOrDefault();
}
public event PropertyChangedEventHandler PropertyChanged;`enter code here`
}
答案 0 :(得分:2)
在Player类中粘贴属性
ObservableCollection<Player> CurrnetIn { get; set; }
并设置此属性,如果您在某个集合中添加播放器。或者我误解了这个问题。
答案 1 :(得分:2)
您可以使用Contains
类的ObservableCollection<T>
方法来确定特定对象是否已在集合中:
if (MainWindow.HomeTeam.Contains(this))
{
MainWindow.HomeTeam.Remove(this);
TotalSelected--;
return;
}
由于您的Player
类没有实现IEquatable<T>
接口,因此您传递给Contains
方法的引用将与集合中的项目进行比较,这些项目完全没问题在这种情况下:
Does List<String>.Contains(mystring) do a reference comparison or a value comparison?