I am creating a WPF basketball application that displays HomeTeamand AwayTeam. I created an object Player, and in the main window I created the ObservableCollectionplayerโs objects for both the home ( HomePlayersList) and the guests ( AwayPlayersList). I used the interface INotifyPropertyChangedfor the player object, so when the IsInGamebool is true, the player is added to one of two ObservableCollection<Player>depending on the quantity. (If list 1 ObservableCollection<Player> HomeTeamor ObservableCollection<Player> AwayTeamcount is 5, then the rest is added to the lookup list ObservableCollection<Team> HomeSubor ObservableCollection<Team> AwaySub.)
But I am trying to distinguish whether a player is in a home or a guest team, and depending on which list the player is on, the player will be added to the new list at home or away.
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; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string KnownName { get; set; }
public string Position { get; set; }
public string Number { get; set; }
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`
}