WPF - 保存文件对话框 [英] WPF - SaveFileDialog
问题描述
我使用 WPF 中的 SaveFileDialog
在用户选择的特定位置导出到 excel 文件.但是在打开 SaveFileDailog
然后用户点击对话框上的 Cancel button
然后我得到另一个对话框,上面写着 你想保存你所做的更改吗?到'Sheet1'?"
然后导出完成"
而不是取消导出.
I am using the SaveFileDialog
in WPF to export into excel file at particular loaction selected by user. But in between when SaveFileDailog
is opened then user clilks on Cancel button
on dialog then i am getting the another dialog that says "Do you want to save changes you made to 'Sheet1'?"
and then "Export completed"
instead of cancelling to export.
那我该怎么办呢?WPF 中的任何类似 'DialogResult'
的东西与 winForms 中的相同吗?
So what i have to do to tackle with it? Any thing in WPF something like 'DialogResult'
that is same as in winForms?
推荐答案
如果用户保存(ShowDialog
方法返回一个可为空的 bool),SaveFileDialog 将返回 true,如果用户按下取消,则返回 false/null.下面是一个示例 MSDN 代码,可帮助您入门:
SaveFileDialog will return true if user saved (the ShowDialog
method returns a nullable bool), and return false/null if user pressed cancel. Below is a sample MSDN code to get you started:
// Configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
}
这篇关于WPF - 保存文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!