private void SelectPrevious() { if (_backgrounds.Any()) { BackgroundListItemViewModel selected = _backgrounds.FirstOrDefault(x => x.IsSelected); foreach (BackgroundListItemViewModel background in _backgrounds) { background.IsSelected = false; } if (selected == null) { _backgrounds[_backgrounds.Count - 1].IsSelected = true; _selectedBackground = new BackgroundViewModel(_backgrounds[_backgrounds.Count - 1].BackgroundModel); } else { int index = Math.Max(_backgrounds.IndexOf(selected) - 1, 0); _backgrounds[index].IsSelected = true; _selectedBackground = new BackgroundViewModel(_backgrounds[index].BackgroundModel); } OnPropertyChanged(nameof(SelectedBackground)); } }
private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == nameof(BackgroundsViewModel.SelectedBackground)) { BackgroundListItemViewModel selected = _viewModel.Backgrounds.FirstOrDefault(x => x.IsSelected); if (selected != null) { if (_tree.ItemContainerGenerator.ContainerFromItem(selected) is TreeViewItem item) { item.BringIntoView(); } } } }
private void Delete() { if (_selectedBackground != null) { bool canDelete = true; foreach (CharacterModel character in _compendium.Characters) { if (character.Background != null && character.Background.Id == _selectedBackground.BackgroundModel.Id) { canDelete = false; break; } } if (canDelete) { string message = String.Format("Are you sure you want to delete {0}?", _selectedBackground.Name); bool?result = _dialogService.ShowConfirmationDialog("Delete Background", message, "Yes", "No", null); if (result == true) { _compendium.DeleteBackground(_selectedBackground.BackgroundModel.Id); BackgroundListItemViewModel listItem = _backgrounds.FirstOrDefault(x => x.BackgroundModel.Id == _selectedBackground.BackgroundModel.Id); if (listItem != null) { _backgrounds.Remove(listItem); } _selectedBackground = null; _compendium.SaveBackgrounds(); OnPropertyChanged(nameof(SelectedBackground)); if (_editBackgroundXML != null) { CancelEditBackground(); } } } else { _dialogService.ShowConfirmationDialog("Unable Delete Background", "Background is in use by a character.", "OK", null, null); } } }
private void SelectBackground(BackgroundListItemViewModel background) { bool selectBackground = true; if (_editBackgroundXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedBackground.Name, Environment.NewLine + Environment.NewLine); string accept = "Save and Continue"; string reject = "Discard Changes"; string cancel = "Cancel Navigation"; bool? result = _dialogService.ShowConfirmationDialog("Unsaved Changes", body, accept, reject, cancel); if (result == true) { if (!SaveEditBackground()) { selectBackground = false; } } else if (result == false) { CancelEditBackground(); } else { selectBackground = false; } } else { CancelEditBackground(); } } if (selectBackground) { foreach (BackgroundListItemViewModel item in _backgrounds) { item.IsSelected = false; } background.IsSelected = true; _selectedBackground = new BackgroundViewModel(background.BackgroundModel); OnPropertyChanged(nameof(SelectedBackground)); } }
private void SortBackgrounds() { if (_backgrounds != null && _backgrounds.Count > 0) { List <BackgroundModel> backgrounds = _backgroundSearchService.Sort(_backgrounds.Select(x => x.BackgroundModel), _backgroundSearchInput.SortOption.Key); for (int i = 0; i < backgrounds.Count; ++i) { if (backgrounds[i].Id != _backgrounds[i].BackgroundModel.Id) { BackgroundListItemViewModel background = _backgrounds.FirstOrDefault(x => x.BackgroundModel.Id == backgrounds[i].Id); if (background != null) { _backgrounds.Move(_backgrounds.IndexOf(background), i); } } } } }
/// <summary> /// Searches, applying current sorting and filtering /// </summary> public void Search() { _backgrounds.Clear(); foreach (BackgroundModel backgroundModel in _backgroundSearchService.Search(_backgroundSearchInput)) { _backgrounds.Add(new BackgroundListItemViewModel(backgroundModel, _stringService)); } if (_selectedBackground != null) { BackgroundListItemViewModel background = _backgrounds.FirstOrDefault(x => x.BackgroundModel.Id == _selectedBackground.BackgroundModel.Id); if (background != null) { background.IsSelected = true; } } OnPropertyChanged(nameof(SortAndFilterHeader)); }
private void Copy() { if (_selectedBackground != null) { bool copyBackground = true; if (_editBackgroundXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedBackground.Name, Environment.NewLine + Environment.NewLine); string accept = "Save and Continue"; string reject = "Discard Changes"; string cancel = "Cancel Navigation"; bool? result = _dialogService.ShowConfirmationDialog("Unsaved Changes", body, accept, reject, cancel); if (result == true) { if (!SaveEditBackground()) { copyBackground = false; } } else if (result == false) { CancelEditBackground(); } else { copyBackground = false; } } else { CancelEditBackground(); } } if (copyBackground) { BackgroundModel newBackground = new BackgroundModel(_selectedBackground.BackgroundModel); newBackground.Name += " (copy)"; newBackground.Id = Guid.NewGuid(); newBackground.XML = newBackground.XML.Replace("<name>" + _selectedBackground.BackgroundModel.Name + "</name>", "<name>" + newBackground.Name + "</name>"); _compendium.AddBackground(newBackground); if (_backgroundSearchService.SearchInputApplies(_backgroundSearchInput, newBackground)) { BackgroundListItemViewModel listItem = new BackgroundListItemViewModel(newBackground, _stringService); _backgrounds.Add(listItem); foreach (BackgroundListItemViewModel item in _backgrounds) { item.IsSelected = false; } listItem.IsSelected = true; } _selectedBackground = new BackgroundViewModel(newBackground); SortBackgrounds(); _compendium.SaveBackgrounds(); OnPropertyChanged(nameof(SelectedBackground)); } } }
private void Add() { bool addBackground = true; if (_editBackgroundXML != null) { if (_editHasUnsavedChanges) { string body = String.Format("{0} has unsaved changes.{1}What would you like to do?", _selectedBackground.Name, Environment.NewLine + Environment.NewLine); string accept = "Save and Continue"; string reject = "Discard Changes"; string cancel = "Cancel Navigation"; bool? result = _dialogService.ShowConfirmationDialog("Unsaved Changes", body, accept, reject, cancel); if (result == true) { if (!SaveEditBackground()) { addBackground = false; } } else if (result == false) { CancelEditBackground(); } else { addBackground = false; } } else { CancelEditBackground(); } } if (addBackground) { string xml = "<name>New Background</name><proficiency></proficiency><trait><name></name><text></text></trait>"; BackgroundModel backgroundModel = _xmlImporter.GetBackground(xml); _compendium.AddBackground(backgroundModel); if (_backgroundSearchService.SearchInputApplies(_backgroundSearchInput, backgroundModel)) { BackgroundListItemViewModel listItem = new BackgroundListItemViewModel(backgroundModel, _stringService); _backgrounds.Add(listItem); foreach (BackgroundListItemViewModel item in _backgrounds) { item.IsSelected = false; } listItem.IsSelected = true; } _selectedBackground = new BackgroundViewModel(backgroundModel); _editBackgroundXML = backgroundModel.XML; SortBackgrounds(); _compendium.SaveBackgrounds(); OnPropertyChanged(nameof(EditingBackgroundXML)); OnPropertyChanged(nameof(IsEditingBackground)); OnPropertyChanged(nameof(SelectedBackground)); } }
private bool SaveEditBackground() { bool saved = false; try { BackgroundModel model = _xmlImporter.GetBackground(_editBackgroundXML); if (model != null) { model.Id = _selectedBackground.BackgroundModel.Id; _compendium.UpdateBackground(model); _selectedBackground = new BackgroundViewModel(model); BackgroundListItemViewModel oldListItem = _backgrounds.FirstOrDefault(x => x.BackgroundModel.Id == model.Id); if (oldListItem != null) { if (_backgroundSearchService.SearchInputApplies(_backgroundSearchInput, model)) { oldListItem.UpdateModel(model); } else { _backgrounds.Remove(oldListItem); } } _editBackgroundXML = null; _editHasUnsavedChanges = false; SortBackgrounds(); _compendium.SaveBackgrounds(); OnPropertyChanged(nameof(SelectedBackground)); OnPropertyChanged(nameof(EditingBackgroundXML)); OnPropertyChanged(nameof(IsEditingBackground)); OnPropertyChanged(nameof(HasUnsavedChanges)); saved = true; } else { string message = String.Format("Something went wrong...{0}{1}{2}{3}", Environment.NewLine + Environment.NewLine, "The following are required:", Environment.NewLine, "-name"); _dialogService.ShowConfirmationDialog("Unable To Save", message, "OK", null, null); } } catch (Exception ex) { string message = String.Format("Something went wrong...{0}{1}", Environment.NewLine + Environment.NewLine, ex.Message); _dialogService.ShowConfirmationDialog("Unable To Save", message, "OK", null, null); } return(saved); }