如何開啟通用對話盒 (WPF .NET)

本文示範如何在 Windows Presentation Foundation (WPF) 中顯示通用系統對話方塊。 Windows 會實作所有應用程式通用的各種可重複使用對話方塊,包括用於選取檔案和列印的對話方塊。

由於這些對話框是由作業系統所提供,所以這些對話框會在作業系統上執行的所有應用程式之間共用。 這些對話框提供一致的用戶體驗,稱為 通用對話方塊。 當使用者在一個應用程式中使用通用對話框時,他們不需要瞭解如何在其他應用程式中使用該對話方塊。

消息框是另一個常見的對話框。 如需詳細資訊,請參閱 如何開啟消息框

開啟檔案對話框

開啟的檔案對話框是由檔案開啟功能用來擷取要開啟之檔案的名稱。

An Open dialog box showing the location to retrieve the file shown from a WPF application.

通用開啟的檔案對話框會實作為 類別, OpenFileDialog 且位於 命名空間中 Microsoft.Win32 。 下列程式代碼示範如何建立、設定及顯示對話框。

// Configure open file dialog box
var dialog = new Microsoft.Win32.OpenFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show open file dialog box
bool? result = dialog.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dialog.FileName;
}
' Configure open file dialog box
Dim dialog As New Microsoft.Win32.OpenFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show open file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open file dialog box results
If result = True Then
    ' Open document
    Dim filename As String = dialog.FileName
End If

如需開啟檔案對話框的詳細資訊,請參閱 Microsoft.Win32.OpenFileDialog

儲存對話方塊

儲存盤案對話框是由檔案儲存功能用來擷取要儲存的檔名。

A Save As dialog box showing the location to save the file shown from a WPF application.

通用儲存盤案對話框會實作為 SaveFileDialog 類別,且位於 命名空間中 Microsoft.Win32 。 下列程式代碼示範如何建立、設定及顯示對話框。

// Configure save file dialog box
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dialog.FileName;
}
' Configure save file dialog box
Dim dialog As New Microsoft.Win32.SaveFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Save document
    Dim filename As String = dialog.FileName
End If

如需 [儲存盤案] 對話框的詳細資訊,請參閱 Microsoft.Win32.SaveFileDialog

開啟資料夾對話框

重要

[開啟資料夾] 對話框適用於 .NET 8.0 和更新版本。

使用者會使用 [開啟資料夾] 對話框來選取一或多個資料夾,並將它們傳回程式。 例如,如果您的程式顯示資料夾的相關信息,例如資料夾中的檔案數量和檔名,您可以使用 [開啟資料夾] 對話框讓使用者選擇資料夾。

An Open Folder dialog box showing the Pictures folder with the Camera Roll folder selected, shown from a WPF application.

通用開啟資料夾對話框會實作為 OpenFolderDialog 類別,且位於 命名空間中 Microsoft.Win32 。 下列程式代碼示範如何建立、設定及顯示對話框。

// Configure open folder dialog box
Microsoft.Win32.OpenFolderDialog dialog = new();

dialog.Multiselect = false;
dialog.Title = "Select a folder";

// Show open folder dialog box
bool? result = dialog.ShowDialog();

// Process open folder dialog box results
if (result == true)
{
    // Get the selected folder
    string fullPathToFolder = dialog.FolderName;
    string folderNameOnly = dialog.SafeFolderName;
}
' Configure open folder dialog box
Dim dialog As New Microsoft.Win32.OpenFolderDialog()

dialog.Multiselect = True
dialog.Title = "Select a folder"

' Show open folder dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open folder dialog box results
If result = True Then

    ' Get multiple folder names
    For index = 0 To dialog.FolderNames.Length
        ' Get the selected folder
        Dim fullPathToFolder As String = dialog.FolderNames(index)
        Dim folderNameOnly As String = dialog.SafeFolderNames(index)
    Next

End If

如需開啟資料夾對話框的詳細資訊,請參閱 Microsoft.Win32.OpenFolderDialog

印表對話框是透過印表功能來選擇並設定使用者想要列印數據的印表機。

A print dialog box shown from a WPF application.

通用列印對話框會實作為 PrintDialog 類別,且位於 命名空間中 System.Windows.Controls 。 下列程式碼示範如何建立、設定及顯示一個對話方塊。

// Configure printer dialog box
var dialog = new System.Windows.Controls.PrintDialog();
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages;
dialog.UserPageRangeEnabled = true;

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Document was printed
}
' Configure printer dialog box
Dim dialog As New System.Windows.Controls.PrintDialog()
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages
dialog.UserPageRangeEnabled = True

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Document was printed
End If

如需列印對話框的詳細資訊,請參閱 System.Windows.Controls.PrintDialog。 如需 WPF 中列印的詳細討論,請參閱 列印概觀

另請參閱