Exemplo n.º 1
0
        private void UpdateSport_Click(object sender, RoutedEventArgs e)
        {
            int indexOfSelectedItem = listSportTeam.Items.IndexOf(listSportTeam.SelectedItem);

            if (indexOfSelectedItem >= 0)
            {
                SportsTeam sportTeamToBeUpdated = SportsTeamList.ElementAt(indexOfSelectedItem);
                window1SportsTeamUpdate = new SportsTeamUpdateWindow(sportTeamToBeUpdated, PersonList);
                window1SportsTeamUpdate.ShowDialog();
                if (window1SportsTeamUpdate.sportsTeam != null)
                {
                    sportTeamToBeUpdated.ID        = window1SportsTeamUpdate.sportsTeam.ID;
                    sportTeamToBeUpdated.PersonID  = window1SportsTeamUpdate.sportsTeam.PersonID;
                    sportTeamToBeUpdated.SportTeam = window1SportsTeamUpdate.sportsTeam.SportTeam;
                    sportTeamToBeUpdated.City      = window1SportsTeamUpdate.sportsTeam.City;

                    listSportTeam.ItemsSource = SportsTeamList;
                    listSportTeam.Items.Refresh();
                }
            }
            else
            {
                MessageBox.Show("Please Select Item");
            }
        }
        public SportsTeamUpdateWindow(SportsTeam sportsTeam, List <Person> personList)
        {
            this.sportsTeam = sportsTeam;
            this.personList = personList;
            InitializeComponent();
            txtId.Text  = sportsTeam.ID.ToString();
            DataContext = this;
            PersonIds   = new ObservableCollection <ComboBoxItem>();
            var personId = new ComboBoxItem
            {
                Content = "<--Select-->"
            };

            PersonIds.Add(personId);
            foreach (var person in this.personList)
            {
                var newpersonID = new ComboBoxItem {
                    Content = person.pID
                };
                if (person.pID == sportsTeam.PersonID)
                {
                    SelectedPersonId = newpersonID;
                }
                PersonIds.Add(newpersonID);
            }
            txtSportTeam.Text = sportsTeam.SportTeam;
            txtCity.Text      = sportsTeam.City;
        }
        private void AddSportsTeam_Click(object sender, RoutedEventArgs e)
        {
            int    id, personID;
            bool   idValid       = int.TryParse(txtId.Text, out id);
            bool   personIDValid = int.TryParse(txtPersonId.Text, out personID);
            string message       = "";

            if (!idValid)
            {
                message += "Id Should be Interger. \n";
            }

            if (!personIDValid)
            {
                message += "Person Id Should Be Selected \n";
            }

            if (txtId.Text != "" &&
                txtPersonId.Text != "" &&
                txtSportTeam.Text != "" &&
                txtCity.Text != "")
            {
                if (personIDValid && idValid)
                {
                    if (sportsTeamList.Exists((sportTeam) => sportTeam.ID == id) == true)
                    {
                        message += "This Id already exists. Please Change Id. \n";
                    }
                    else
                    {
                        newSportsTeam = new SportsTeam()
                        {
                            ID        = id,
                            PersonID  = personID,
                            SportTeam = txtSportTeam.Text,
                            City      = txtCity.Text
                        };
                    }
                }
            }
            else
            {
                message += "All Text Boxes Should Be Filed";
            }
            if (message.Length > 0)
            {
                MessageBox.Show(message);
            }
            Close();
        }
        private void UpdateSportsTeam_Click(object sender, RoutedEventArgs e)
        {
            if (txtId.Text != "" &&
                txtPersonId.Text != "" &&
                txtSportTeam.Text != "" &&
                txtCity.Text != "")
            {
                sportsTeam = new SportsTeam()
                {
                    ID        = int.Parse(txtId.Text),
                    PersonID  = int.Parse(txtPersonId.Text),
                    SportTeam = txtSportTeam.Text,
                    City      = txtCity.Text
                };
            }
            else
            {
                MessageBox.Show("All text boes should be filled.");
            }

            Close();
        }