65.9K
CodeProject 正在变化。 阅读更多。
Home

一个可自定义的 WPF TaskDialog

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.54/5 (22投票s)

2008年11月3日

CPOL

3分钟阅读

viewsIcon

113670

downloadIcon

2133

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

TaskDialog

引言

这是WPF平台的Vista TaskDialog的实现。

MessageBox

通过调用Show方法的static重载,TaskDialog可以作为MessageBox的替代品。 它有四个文本属性:Header(标题), Content(内容), Detail(详情), 和 Footer(页脚)。 Detail是一个可折叠的区域。 HeaderFooter都有一个图标属性 (分别为HeaderIcon, FooterIcon),并且HeaderBackground(背景)和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, DetailFooter属性。

要自定义对话框,你需要创建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类, HeaderContent属性就来自这里。 除此之外,还添加了DetailFooter属性。 这些属性的类型是 object 并且有它们自己的模板属性 (HeaderTemplate, ContentTemplate, DetailTemplate, 和 FooterTemplate)。 TaskDialog为文本内容设置了默认的数据模板,你可以使用上面的模板属性替换这些默认模板,这样你可以用任何你喜欢的方式格式化文本。 下图展示了用斜体和下划线格式化文本的例子。

TaskDialog6.jpg

// 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, DetailFooter的类型是 object,你不限于文本,你可以将任何你想要的内容放到TaskDialog上。 下面是一个TaskDialog看起来像Vista用户帐户控制提示的例子。 Content属性是一个UserControl,其中包含一个图像和一些文本以及两个CommandButton(这些只是具有自定义样式和添加的Header属性的普通按钮),它们包含在TaskDialog中。

TaskDialog3.jpg

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

TaskDialog5.jpg

其他的感兴趣的属性

这是TaskDialog暴露的属性列表(按字母顺序排列)

  • Button1Text: 类型 string. 设置当TaskDialogButton属性设置为Custom时按钮的文本。
  • Button2Text: 同上
  • Button3Text: 同上
  • DefaultResult: 类型 TaskDialogResult. 设置默认按钮。
  • IsButton1Enabled: 类型 bool. Getset 按钮的Enabled状态。
  • IsButton2Enabled: 与上面相同。
  • IsButton3Enabled: 与上面相同。
  • IsCloseButtonEnabled: 类型 bool. Getset 窗口关闭按钮的Enabled状态(默认为false
  • IsExpanded: 类型 bool. Getset Detail部分的可见性。
  • IsModal: 类型 bool. Getset 对话框窗口是否为模态窗口。
  • ShowInTaskBar: 类型 bool. Getset 对话框窗口是否在任务栏中显示。
  • 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日:更新源代码
© . All rights reserved.