Click here to Skip to main content
Click here to Skip to main content

A Customizable WPF TaskDialog

By , 8 Apr 2009
 
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

alrh
Software Developer
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMaking Show() static is MVVM unfriendlymemberDave Midgley13 Mar '12 - 6:45 
QuestionHide the show details when there is no details to showmemberSebastien Charest31 Jan '11 - 10:23 
GeneralExtended WPF toolkit has now message box control.membermammadkoma6 Nov '10 - 21:35 
QuestionHow to get the DialogResult from UAC usercontrolmember-=barmaley=-20 Jan '10 - 9:31 
GeneralProject reference problemmemberRichard Deeming7 Apr '09 - 9:38 
GeneralRe: Project reference problemmemberalrh7 Apr '09 - 22:47 
GeneralRe: Project reference problemmemberRichard Deeming8 Apr '09 - 1:11 
GeneralBug in OnButtonPreviewKeyDownmemberdanoul6 Apr '09 - 1:00 
GeneralRe: Bug in OnButtonPreviewKeyDownmemberalrh6 Apr '09 - 1:56 
GeneralError in your code: Template PARTsmemberdanoul6 Apr '09 - 0:56 
GeneralRe: Error in your code: Template PARTsmemberdanoul6 Apr '09 - 0:58 
GeneralRe: Error in your code: Template PARTsmemberalrh6 Apr '09 - 1:55 
GeneralProposal for handling ESC keymemberdanoul5 Apr '09 - 23:21 
GeneralRe: Proposal for handling ESC keymemberalrh6 Apr '09 - 1:48 
GeneralRe: Proposal for handling ESC keymemberdanoul6 Apr '09 - 8:50 
GeneralRe: Proposal for handling ESC keymemberalrh8 Apr '09 - 22:48 
QuestionCenter Customizable WPF TaskDialog with its parent window [modified]memberRoberto Jucá3 Apr '09 - 11:26 
AnswerRe: Center Customizable WPF TaskDialog with its parent windowmemberalrh4 Apr '09 - 22:33 
GeneralRe: Center Customizable WPF TaskDialog with its parent window [modified]memberRoberto Jucá5 Apr '09 - 8:00 
GeneralRe: Center Customizable WPF TaskDialog with its parent windowmemberalrh5 Apr '09 - 22:56 
GeneralMulti-threaded versionmembervic_ch200018 Jan '09 - 3:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 8 Apr 2009
Article Copyright 2008 by alrh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid