在WPF中,系統定義的對話框(Dialog)有三種:
(1)Microsoft.Win32.OpenFileDialog
(2)Microsoft.Win32.SaveFileDialog
(3)System.Windows.Controls.PrintDialog
先看看前面兩種的層次繼承關系:
System.Object
Microsoft.Win32.CommonDialog
Microsoft.Win32.FileDialog
Microsoft.Win32.OpenFileDialog
Microsoft.Win32.SaveFileDialog
我們看到,這兩種對話框均位於Microsoft.Win32命名空間中,它們均繼承自Microsoft.Win32.FileDialog。
(1)OpenFileDialog:
OpenFileDialog有一種公共事件:FileOk(當用戶選擇文件名,點擊打開按鈕時發生)
OpenFileDialog有以下幾種公共方法:
OpenFile():打開單個文件
OpenFiles():打開多個文件
Reset():重置
ShowDialog():顯示對話框
OpenFileDialog的公共屬性有:
.AddExtension,
.CheckFileExists,
.CheckPathExists,
.DefaultExt,
.DereferenceLinks,
.FileName,
.FileNames,
.Filter,
.FilterIndex,
.InitialDirectory,
.Multiselect,
.ReadOnlyChecked,
.RestoreDirectory,
.SafeFileName,
.SafeFileNames,
.ShowReadOnly,
.Tag,
.Title,
.ValidateNames
其中,我們最常用的有: FileName, FileNames, Filter, FilterIndex, DefaultExt,InitialDirectory等。
(2) SaveFileDialog:
與OpenFileDialog類似,SaveFileDialog公共方法:
OpenFile()
Reset()
ShowDialog()
公共屬性:
.AddExtension,
.CheckFileExists,
.CheckPathExists,
.CreatePrompt,
.DefaultExt,
.DereferenceLinks,
.FileName,
.FileNames,
.Filter,
.FilterIndex,
.InitialDirectory,
.OverwritePrompt,
.RestoreDirectory,
.SafeFileName,
.SafeFileNames,
.Tag,
.Title,
.ValidateNames
說明:在WPF中的Microsoft.Win32.OpenFileDialog,Microsoft.Win32.SaveFileDialog與在WinForm中的System.Windows.Forms.OpenFileDialog, System.Windows.Forms.SaveFileDialog用法上基本上一樣,但前者由於WPF的限制,無法在Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE下使用。
再看看看PrintDialog:
(3)PrintDialog:
看看它的繼承關系:
System.Object
System.Windows.Controls.PrintDialog
需要注意的是:不要將System.Windows.Controls.PrintDialog與System.Windows.Forms.PrintDialog混淆了,后者用於WinForm應用程序中。System.Windows.Controls.PrintDialog則用於Windows Presentation Foundation (WPF)應用程序中。