private void Add() { NewPlayerWindow npw = new NewPlayerWindow(); Profile newProfile = new Profile(); npw.DataContext = newProfile; if (npw.ShowDialog() == true) { newProfile.Image = "https://randomuser.me/api/portraits/men/"; newProfile.Image += new Random().Next(0, 60).ToString() + ".jpg"; ProfileCollection.Insert(newProfile, false); } // alapból a lista végére rakunk (false) // itt most jól is jön ki mert a GUI-nál trükközni kéne, hogy a lista elejére való beszúráskor ott is jelenjen meg // eddig ez nem jött elő, mert list és obscoll esetén is a végére rak a .Add }
private void Button_Click(object sender, RoutedEventArgs e) { NewPlayerWindow window = new NewPlayerWindow(Users); if (window.ShowDialog() == true) { UserViewModel userViewModel = new UserViewModel(eventAggregator, kb) { FirstName = window.PlayerFirstName, LastName = window.PlayerLastName, ButtonId = window.ButtonId, Score = 0 }; Users.Add(userViewModel); eventAggregator.GetEvent <PubSubEvent <UserViewModel> >().Publish(userViewModel); } }
public TeamViewModel(Team t) { playersRepository = MainViewModel.Instance.PlayersRepository; teamsRepository = MainViewModel.Instance.TeamsRepository; Team = t; TeamPlayers = Team.Players.ToList(); AddPlayerCommand = new RelayCommand(() => { NewPlayerWindow newPlayerWindow = new NewPlayerWindow(); // Show window modally // NOTE: Returns only when window is closed bool?dialogResult = newPlayerWindow.ShowDialog(); if (dialogResult == true) { Player p = new Player() { Name = newPlayerWindow.PlayerName.Text, Nickname = newPlayerWindow.PlayerNickname.Text, TeamId = Team.Id }; playersRepository.Create(p); UpdateTeamPlayers(); } }); EditPlayerCommand = new RelayCommand(() => { if (SelectedPlayer == null) { MessageBox.Show("Ничего не выбрано"); return; } EditPlayerWindow editPlayerWindow = new EditPlayerWindow(); editPlayerWindow.PlayerName.Text = SelectedPlayer.Name; editPlayerWindow.PlayerNickname.Text = SelectedPlayer.Nickname; editPlayerWindow.PlayerTeamId.Text = SelectedPlayer.TeamId.ToString(); bool?dialogResult = editPlayerWindow.ShowDialog(); if (dialogResult == true) { int newId = int.Parse(editPlayerWindow.PlayerTeamId.Text); Team newTeam = teamsRepository.Get(team => team.Id == newId).SingleOrDefault(); if (newTeam == null) { MessageBox.Show("Такой команды не существует\nПроверьте Id"); return; } if (newId != SelectedPlayer.TeamId) { SelectedPlayer.TeamId = newId; SelectedPlayer.Team = null; } SelectedPlayer.Name = editPlayerWindow.PlayerName.Text; SelectedPlayer.Nickname = editPlayerWindow.PlayerNickname.Text; playersRepository.Update(SelectedPlayer); UpdateTeamPlayers(); } }); RemovePlayerCommand = new RelayCommand(() => { if (SelectedPlayer == null) { return; } playersRepository.Remove(SelectedPlayer); // Update list of teams to get fresh info about all teams MainViewModel.Instance.UpdateTeamsList(); // load fresh info about players in selected team TeamPlayers = playersRepository.GetPlayersWithTeams(player => player.TeamId == Team.Id).ToList(); }); RefreshPlayersListCommand = new RelayCommand(() => TeamPlayers = playersRepository.GetPlayersWithTeams(player => player.TeamId == Team.Id)); SortPlayersByNameCommand = new RelayCommand(() => TeamPlayers = TeamPlayers.OrderBy(player => player.Name).ToList()); SortPlayersByNicknameCommand = new RelayCommand(() => TeamPlayers = TeamPlayers.OrderBy(player => player.Nickname).ToList()); SortPlayersByIdCommand = new RelayCommand(() => TeamPlayers = TeamPlayers.OrderBy(player => player.Id).ToList()); }