private void populatePlayer2Combo()
        {
            var oldPlayer2 = cmbPlayer2.SelectedIndex != -1 ? cmbPlayer2.SelectedItem.ToString() : "";

            PlayersComboList2.Clear();
            if (cmbEventType.SelectedIndex == -1)
            {
                return;
            }
            var curEventKey = PlayByPlayEntry.EventTypes.Single(pair => pair.Value == cmbEventType.SelectedItem.ToString()).Key;

            if (curEventKey <= 0 || cmbPlayer1.SelectedIndex == -1)
            {
                PlayersComboList2 = new ObservableCollection <ComboBoxItemWithIsEnabled>(PlayersComboList);
            }
            else
            {
                var curPlayer = cmbPlayer1.SelectedItem as ComboBoxItemWithIsEnabled;
                if (curPlayer == null)
                {
                    cmbPlayer2.ItemsSource = PlayersComboList2;
                    return;
                }
                var curPlayerTeam = _bse.PBSList.Single(pbs => pbs.PlayerID == curPlayer.ID).TeamID;
                List <PlayerStats> list;
                if (PlayByPlayEntry.UseOpposingTeamAsPlayer2.Contains(curEventKey))
                {
                    if (curPlayerTeam == _t1ID)
                    {
                        PlayersComboList2.Add(new ComboBoxItemWithIsEnabled(txbHomeTeam.Text, false));
                        list = HomeActive.ToList();
                    }
                    else
                    {
                        PlayersComboList2.Add(new ComboBoxItemWithIsEnabled(txbAwayTeam.Text, false));
                        list = AwayActive.ToList();
                    }
                }
                else
                {
                    if (curPlayerTeam == _t1ID)
                    {
                        PlayersComboList2.Add(new ComboBoxItemWithIsEnabled(txbAwayTeam.Text, false));
                        list = curEventKey != 13 ? AwayActive.Where(ps => ps.ID != curPlayer.ID).ToList() : AwayActive.ToList();
                    }
                    else
                    {
                        PlayersComboList2.Add(new ComboBoxItemWithIsEnabled(txbHomeTeam.Text, false));
                        list = curEventKey != 13 ? HomeActive.Where(ps => ps.ID != curPlayer.ID).ToList() : HomeActive.ToList();
                    }
                }
                list.ForEach(ps => PlayersComboList2.Add(new ComboBoxItemWithIsEnabled(ps.ToString(), true, ps.ID)));
            }
            cmbPlayer2.ItemsSource = PlayersComboList2;

            if (cmbPlayer2.ItemsSource.Cast <ComboBoxItemWithIsEnabled>().Any(o => o.Item == oldPlayer2))
            {
                cmbPlayer2.SelectedItem = cmbPlayer2.ItemsSource.Cast <ComboBoxItemWithIsEnabled>().Single(o => o.Item == oldPlayer2);
            }
        }
        private void sortPlayerLists()
        {
            AwaySubs.Sort((ps1, ps2) => String.Compare(ps1.FullName, ps2.FullName, StringComparison.CurrentCultureIgnoreCase));
            AwayActive.Sort((ps1, ps2) => String.Compare(ps1.FullName, ps2.FullName, StringComparison.CurrentCultureIgnoreCase));
            HomeSubs.Sort((ps1, ps2) => String.Compare(ps1.FullName, ps2.FullName, StringComparison.CurrentCultureIgnoreCase));
            HomeActive.Sort((ps1, ps2) => String.Compare(ps1.FullName, ps2.FullName, StringComparison.CurrentCultureIgnoreCase));

            PlayersComboList.Clear();
            PlayersComboList.Add(new ComboBoxItemWithIsEnabled(txbAwayTeam.Text, false));
            AwayActive.ToList().ForEach(ps => PlayersComboList.Add(new ComboBoxItemWithIsEnabled(ps.ToString(), true, ps.ID)));
            PlayersComboList.Add(new ComboBoxItemWithIsEnabled(txbHomeTeam.Text, false));
            HomeActive.ToList().ForEach(ps => PlayersComboList.Add(new ComboBoxItemWithIsEnabled(ps.ToString(), true, ps.ID)));

            populatePlayer2Combo();
        }
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (dgEvents.SelectedIndex == -1)
            {
                return;
            }
            if (btnEdit.Content.ToString() == "Edit")
            {
                var selectedPlay = dgEvents.SelectedItem as PlayByPlayEntry;
                dgEvents.IsEnabled  = false;
                btnSave.IsEnabled   = false;
                btnCancel.IsEnabled = false;
                btnAdd.IsEnabled    = false;
                btnDelete.IsEnabled = false;
                _shotClockTimer.Stop();
                _timeLeftTimer.Stop();
                _savedTimeLeft   = _timeLeft;
                _savedShotClock  = _shotClock;
                _savedAwayActive = AwayActive.ToList();
                _savedAwaySubs   = AwaySubs.ToList();
                _savedHomeActive = HomeActive.ToList();
                _savedHomeSubs   = HomeSubs.ToList();
                _savedAwayPoints = Convert.ToInt32(AwayPoints);
                _savedHomePoints = Convert.ToInt32(HomePoints);
                _savedPeriod     = CurrentPeriod;

                _timeLeft = selectedPlay.TimeLeft;
                updateTimeLeftIndication(_timeLeft);

                _shotClock = selectedPlay.ShotClockLeft;
                updateShotClockIndication(_shotClock);

                AwayActive = new ObservableCollection <PlayerStats>(selectedPlay.Team1PlayerIDs.Select(id => _pst[id]).ToList());
                AwaySubs   =
                    new ObservableCollection <PlayerStats>(
                        _bse.PBSList.Where(pbs => pbs.TeamID == _t1ID && !selectedPlay.Team1PlayerIDs.Contains(pbs.PlayerID))
                        .Select(pbs => _pst[pbs.PlayerID])
                        .ToList());
                HomeActive = new ObservableCollection <PlayerStats>(selectedPlay.Team2PlayerIDs.Select(id => _pst[id]).ToList());
                HomeSubs   =
                    new ObservableCollection <PlayerStats>(
                        _bse.PBSList.Where(pbs => pbs.TeamID == _t2ID && !selectedPlay.Team2PlayerIDs.Contains(pbs.PlayerID))
                        .Select(pbs => _pst[pbs.PlayerID])
                        .ToList());

                lstAwayActive.ItemsSource = AwayActive;
                lstAwaySubs.ItemsSource   = AwaySubs;
                lstHomeActive.ItemsSource = HomeActive;
                lstHomeSubs.ItemsSource   = HomeSubs;

                sortPlayerLists();

                cmbEventType.SelectedItem = PlayByPlayEntry.EventTypes[selectedPlay.EventType];
                txtEventDesc.Text         = selectedPlay.EventDesc;
                cmbPlayer1.SelectedItem   = PlayersComboList.Single(item => item.ID == selectedPlay.Player1ID);
                cmbPlayer2.SelectedItem   = selectedPlay.Player2ID != -1
                                              ? PlayersComboList2.Single(item => item.ID == selectedPlay.Player2ID)
                                              : null;
                cmbLocationShotDistance.SelectedItem = selectedPlay.EventType != PlayByPlayEntry.ShotAttemptEventType
                                                           ? PlayByPlayEntry.EventLocations[selectedPlay.Location]
                                                           : ShotEntry.ShotDistances[selectedPlay.ShotEntry.Distance];
                txtLocationDesc.Text       = selectedPlay.LocationDesc;
                cmbShotOrigin.SelectedItem = selectedPlay.EventType == PlayByPlayEntry.ShotAttemptEventType
                                                 ? ShotEntry.ShotOrigins[selectedPlay.ShotEntry.Origin]
                                                 : null;
                cmbShotType.SelectedItem = selectedPlay.EventType == PlayByPlayEntry.ShotAttemptEventType
                                               ? ShotEntry.ShotTypes[selectedPlay.ShotEntry.Type]
                                               : null;
                chkShotIsMade.IsChecked = selectedPlay.EventType == PlayByPlayEntry.ShotAttemptEventType &&
                                          selectedPlay.ShotEntry.IsMade;
                chkShotIsAssisted.IsChecked = selectedPlay.EventType == PlayByPlayEntry.ShotAttemptEventType &&
                                              selectedPlay.ShotEntry.IsAssisted;
                AwayPoints    = selectedPlay.T1PTS;
                HomePoints    = selectedPlay.T2PTS;
                CurrentPeriod = selectedPlay.Quarter;

                btnEdit.Content = "Save";
            }
            else
            {
                if (cmbEventType.SelectedIndex == -1 || cmbPlayer1.SelectedIndex == -1 || cmbLocationShotDistance.SelectedIndex == -1)
                {
                    return;
                }
                if (grdShotEvent.IsEnabled && (cmbShotOrigin.SelectedIndex == -1 || cmbShotType.SelectedIndex == -1))
                {
                    return;
                }
                var play = createPlayByPlayEntryFromCurrent();
                Plays.Remove(dgEvents.SelectedItem as PlayByPlayEntry);
                Plays.Add(play);
                Plays.Sort(new PlayByPlayEntryComparerAsc());

                _timeLeft = _savedTimeLeft;
                updateTimeLeftIndication(_timeLeft);
                _shotClock = _savedShotClock;
                updateShotClockIndication(_shotClock);
                AwayActive    = new ObservableCollection <PlayerStats>(_savedAwayActive);
                AwaySubs      = new ObservableCollection <PlayerStats>(_savedAwaySubs);
                HomeActive    = new ObservableCollection <PlayerStats>(_savedHomeActive);
                HomeSubs      = new ObservableCollection <PlayerStats>(_savedHomeSubs);
                AwayPoints    = _savedAwayPoints;
                HomePoints    = _savedHomePoints;
                CurrentPeriod = _savedPeriod;

                lstAwayActive.ItemsSource = AwayActive;
                lstAwaySubs.ItemsSource   = AwaySubs;
                lstHomeActive.ItemsSource = HomeActive;
                lstHomeSubs.ItemsSource   = HomeSubs;

                sortPlayerLists();

                dgEvents.IsEnabled  = true;
                btnSave.IsEnabled   = true;
                btnCancel.IsEnabled = true;
                btnAdd.IsEnabled    = true;
                btnDelete.IsEnabled = true;

                btnEdit.Content = "Edit";
            }
        }