private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (m_ShouldClose == null) { // mark comment to m_ShouldClose e.Cancel = true; // is here unsaved documents? bool bUnsaved = false; foreach (Document doc in m_VM.DocManager.OpenDocuments) { if (doc.ChangesCount > 0) { bUnsaved = true; break; } } // if (bUnsaved) { SaveChangesDialog_ViewModel vm = new SaveChangesDialog_ViewModel(); vm.Text = "There are unsaved documents. Some data will be lost possibly."; vm.IsSaveButtonVisible = false; SaveChangesDialog saveChangesDialog = new SaveChangesDialog(vm); //show the dialog // true - save // false - cancel // null - continue var result = await DialogHost.Show(saveChangesDialog); if (result is bool && !(bool)result) { return; } } m_ShouldClose = true; Application.Current.Shutdown(); } }
//============================================================================= private async void CloseDocumentButton_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; if (btn == null) { return; } ListBoxItem lbi = Utils.TryFindParent <ListBoxItem>(btn); if (lbi == null) { return; } Document docToClose = lbi.DataContext as Document; if (docToClose == null) { return; } if (docToClose.ChangesCount > 0) { StringBuilder sb = new StringBuilder(); sb.Append("Document \""); sb.Append(docToClose.DisplayName); sb.Append("\" has changes. Do you want to save changes?"); SaveChangesDialog_ViewModel vm = new SaveChangesDialog_ViewModel(); vm.Text = sb.ToString(); SaveChangesDialog saveChangesDialog = new SaveChangesDialog(vm); //show the dialog // true - save // false - cancel // null - continue var result = await DialogHost.Show(saveChangesDialog); if (result is bool) { bool bRes = (bool)result; // cancel - dont close document if (!bRes) { return; } // save doc if (docToClose.IsItNewDocument) { string strFilePath = _GetPath(); if (string.IsNullOrEmpty(strFilePath)) { return; } docToClose.Save(strFilePath); } else { docToClose.Save(); } } } // select document int iOldIndex = m_VM.DocManager.OpenDocuments.IndexOf(docToClose); m_VM.DocManager.OpenDocuments.Remove(docToClose); if (m_VM.DocManager.OpenDocuments.Count > 0) { if (iOldIndex >= m_VM.DocManager.OpenDocuments.Count) { iOldIndex = m_VM.DocManager.OpenDocuments.Count - 1; } Document docToSelect = m_VM.DocManager.OpenDocuments[iOldIndex]; if (docToSelect != null) { docToSelect.IsSelected = true; } } }