一个可自定义的 WPF TaskDialog
一个用 C# 编写的 WPF TaskDialog 控件,模拟 Vista TaskDialog。

引言
这是WPF平台的Vista TaskDialog
的实现。
MessageBox
通过调用Show
方法的static
重载,TaskDialog
可以作为MessageBox
的替代品。 它有四个文本属性:Header
(标题), Content
(内容), Detail
(详情), 和 Footer
(页脚)。 Detail
是一个可折叠的区域。 Header
和Footer
都有一个图标属性 (分别为HeaderIcon
, FooterIcon
),并且Header
有Background
(背景)和Foreground
(前景)属性。
// TaskDialog.Show method signature
public static TaskDialogResult Show(
string title,
string header,
string content,
string detail,
string footer,
TaskDialogButton button,
TaskDialogResult defaultResult,
TaskDialogIcon headerIcon,
TaskDialogIcon footerIcon,
Brush headerBackground,
Brush headerForeground)
// TaskDialog.Show method example
TaskDialog.Show("Task Dialog Options",
"The Header text for the message box",
"The Content text for the message box. this" +
" text will automatically wrap and is selectable.",
"The Detail text for the message box. this text " +
"will automatically wrap and is selectable",
"The Footer text for the message box.",
TaskDialogButton.Ok,
TaskDialogResult.None,
TaskDialogIcon.Information,
TaskDialogIcon.Shield,
Brushes.White,
Brushes.Navy);
自定义TaskDialog
使用 static Show
方法,你只能将string
传递给Header
, Content
, Detail
和Footer
属性。
要自定义对话框,你需要创建TaskDialog
类的一个实例,设置一些属性,然后调用Show
方法。
// TaskDialog Instance example
TaskDialog dialog = new TaskDialog();
dialog.Title = "TaskDialog example";
dialog.HeaderIcon = TaskDialogIcon.Warning;
dialog.SystemSound = TaskDialogSound.Exclamation;
// header properties
dialog.Header = "This is the Header.";
dialog.HeaderBackground = Brushes.DarkGray;
dialog.HeaderForeground = Brushes.White;
// Content, Detail and Footer
dialog.Content = "This is the content";
dialog.Detail = "This is the detail";
dialog.Footer = "this is the Footer";
dialog.Show();
TaskDialog
控件派生自HeaderedContentControl
类, Header
和Content
属性就来自这里。 除此之外,还添加了Detail
和Footer
属性。 这些属性的类型是 object
并且有它们自己的模板属性 (HeaderTemplate
, ContentTemplate
, DetailTemplate
, 和 FooterTemplate
)。 TaskDialog
为文本内容设置了默认的数据模板,你可以使用上面的模板属性替换这些默认模板,这样你可以用任何你喜欢的方式格式化文本。 下图展示了用斜体和下划线格式化文本的例子。
// DataTemplate for the content property above
<DataTemplate x:Key="_customContentDataTemplate">
<TextBlock Text="{Binding Content,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Controls:TaskDialog}}}"
FontStyle="Italic"
TextDecorations="Underline"
TextWrapping="Wrap"/>
</DataTemplate>
因为Header
, Content
, Detail
和Footer
的类型是 object
,你不限于文本,你可以将任何你想要的内容放到TaskDialog
上。 下面是一个TaskDialog
看起来像Vista用户帐户控制提示的例子。 Content
属性是一个UserControl
,其中包含一个图像和一些文本以及两个CommandButton
(这些只是具有自定义样式和添加的Header
属性的普通按钮),它们包含在TaskDialog
中。

一个模拟Vista文件复制窗口的TaskDialog

其他的感兴趣的属性
这是TaskDialog
暴露的属性列表(按字母顺序排列)
Button1Text
: 类型string
. 设置当TaskDialogButton
属性设置为Custom
时按钮的文本。Button2Text
: 同上Button3Text
: 同上DefaultResult
: 类型TaskDialogResult
. 设置默认按钮。IsButton1Enabled
: 类型bool
.Get
或set
按钮的Enabled
状态。IsButton2Enabled
: 与上面相同。IsButton3Enabled
: 与上面相同。IsCloseButtonEnabled
: 类型bool
.Get
或set
窗口关闭按钮的Enabled
状态(默认为false
)IsExpanded
: 类型bool
.Get
或set
Detail
部分的可见性。IsModal
: 类型bool
.Get
或set
对话框窗口是否为模态窗口。ShowInTaskBar
: 类型bool
.Get
或set
对话框窗口是否在任务栏中显示。SystemSound
: 类型TaskDialogSound
.Set
当窗口显示时播放的系统声音。TaskDialogButton
: 类型TaskDialogButton
. 在窗口上显示的按钮。 可以是None
,Ok
,OkCancel
,YesNo
,YesNoCancel
, 或Custom
(有关设置按钮标题,请参见上文)。Title
: 类型string
. 窗口标题。ToggleButtonTexts
: 这需要一个类型为TaskDialogToggleButtonTexts
的类,该类提供了两个属性,用于设置折叠和展开状态下切换按钮的文本(默认为“显示详细信息”和“隐藏详细信息”)。TopMost
: 设置窗口的ZOrder
。
演示
下载内容包括一个演示TaskDialog
的项目。 查看 *Window1.xaml.cs* 中的按钮事件处理程序,了解如何创建每个TaskDialog
。
历史
- 2008年11月3日:首次发布
- 2008年11月7日:更新源代码
- 2009年4月4日:更新源代码
- 2009年4月7日:更新源代码