Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Customizable WPF TaskDialog

0.00/5 (No votes)
8 Apr 2009 1  
A WPF TaskDialog control written in C# that simulates the Vista TaskDialog
TaskDialog

Introduction

This is an implementation of the Vista TaskDialog for WPF.

MessageBox

By invoking one of the Show method's static overloads, the TaskDialog acts as a replacement for the MessageBox. It has four text properties: Header, Content, Detail, and Footer. Detail is a collapsed region. The Header and Footer both have an icon property (HeaderIcon, FooterIcon, respectively), and the Header has Background and Foreground properties.

// 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);

Customizing the TaskDialog

Using the static Show method, you are limited to only passing strings to the Header, Content, Detail, and Footer properties.

To customize the dialog, you need to create an instance of the TaskDialog class, set some properties, and call the Show method.

// 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();

The TaskDialog control derives from the HeaderedContentControl class, which is where we get the Header and Content properties. Added to this are the Detail and Footer properties. These properties are of type object and have their own template properties (HeaderTemplate, ContentTemplate, DetailTemplate, and FooterTemplate). The TaskDialog has default data templates for text content which you can replace using the template properties above, so you can format text in any way you like. The following picture shows this by formatting the text with italics and underlines:

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>

Because the Header, Content, Detail, and Footer are of type object, you are not limited to text, you can put anything you want on the TaskDialog. Below is an example of the TaskDialog looking like a Vista User Account Control prompt. The Content property is a UserControl which has an image and some text and two CommandButtons (these are just normal buttons with a custom style and an added Header property) which are included in the TaskDialog.

TaskDialog3.jpg

A TaskDialog simulating the Vista FileCopy window:

TaskDialog5.jpg

Additional Properties of Interest

This is a list of properties the TaskDialog exposes (in alphabetical order):

  • Button1Text: Type string. Sets the text of the button when the TaskDialogButton property is set to Custom
  • Button2Text: As above
  • Button3Text: As above
  • DefaultResult: Type TaskDialogResult. Sets the default button
  • IsButton1Enabled: Type bool. Get or set the Enabled state of the button
  • IsButton2Enabled: Same as above
  • IsButton3Enabled: Same as above
  • IsCloseButtonEnabled: Type bool. Get or set the Enabled state of the window close button (default is false)
  • IsExpanded: Type bool. Get or set the visibility of the Detail section
  • IsModal: Type bool. Get or set whether the dialog window is modal
  • ShowInTaskBar: Type bool. Get or set whether the dialog window displays in the taskbar
  • SystemSound: Type TaskDialogSound. Set the system sound to play when the window is shown
  • TaskDialogButton: Type TaskDialogButton. The buttons to display on the window. Can be None, Ok, OkCancel, YesNo, YesNoCancel, or Custom (see above for setting button captions)
  • Title: Type string. The window title
  • ToggleButtonTexts: This takes a class of type TaskDialogToggleButtonTexts which provides two properties for setting the text of the toggle button in its collapsed and expanded states (defaults to 'Show Details' and 'Hide Details').
  • TopMost: Set the ZOrder of the window

Demo

The download includes a project that demonstrates the TaskDialog. Check out the button event handlers in Window1.xaml.cs to see how each TaskDialog is created.

History

  • 3rd November, 2008: Initial post
  • 7th November, 2008: Updated source code
  • 4th April, 2009: Updated source code
  • 7th April, 2009: Updated source code

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here