Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / WPF

A Customizable WPF TaskDialog

Rate me:
Please Sign up or sign in to vote.
4.54/5 (23 votes)
8 Apr 2009CPOL3 min read 110.2K   2.1K   91   23
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.

C#
// 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.

C#
// 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

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


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow can I use it with .NET CORE APP? Pin
Sandy Brun16-Nov-20 10:20
Sandy Brun16-Nov-20 10:20 
Questioni want only exception msg box so what is required files?? Pin
Member 105791221-Jun-15 21:33
Member 105791221-Jun-15 21:33 
QuestionMaking Show() static is MVVM unfriendly Pin
Dave Midgley13-Mar-12 6:45
Dave Midgley13-Mar-12 6:45 
QuestionHide the show details when there is no details to show Pin
Sebastien Charest31-Jan-11 10:23
Sebastien Charest31-Jan-11 10:23 
GeneralExtended WPF toolkit has now message box control. Pin
mammadkoma6-Nov-10 21:35
mammadkoma6-Nov-10 21:35 
QuestionHow to get the DialogResult from UAC usercontrol Pin
-=barmaley=-20-Jan-10 9:31
-=barmaley=-20-Jan-10 9:31 
Thank you alrh, this is a great job!
How can i get the DialogResult from UAC usercontrol?

Uac.xaml.cs----------------------------------
buttonCancel.Click +=
delegate
{
??????= TaskDialogResult.Cancel;
_parentWindow.Close();
};

buttonAllow.Click +=
delegate
{
??????= TaskDialogResult.Ok;
_parentWindow.Close();
};


Window1.xaml.cs------------------------------
private void UAC1_Click(object sender, RoutedEventArgs e)
{
TaskDialog dialog = new TaskDialog();
Uac uac1 = new Uac(dialog.TaskDialogWindow);
dialog.Content = uac1;
// for the body,use a custom Usercontrol to display an image with the text
dialog.Detail = @"C:\Program Files\Some program.exe";
dialog.Footer = "User Account Control helps stop unauthorised changes to your computer";

if(dialog.Show()=TaskDialogResult.Ok); <======== DialogResult???
}
GeneralProject reference problem Pin
Richard Deeming7-Apr-09 9:38
mveRichard Deeming7-Apr-09 9:38 
GeneralRe: Project reference problem Pin
alrh7-Apr-09 22:47
alrh7-Apr-09 22:47 
GeneralRe: Project reference problem Pin
Richard Deeming8-Apr-09 1:11
mveRichard Deeming8-Apr-09 1:11 
GeneralBug in OnButtonPreviewKeyDown Pin
Daniel Weck6-Apr-09 1:00
Daniel Weck6-Apr-09 1:00 
GeneralRe: Bug in OnButtonPreviewKeyDown Pin
alrh6-Apr-09 1:56
alrh6-Apr-09 1:56 
GeneralError in your code: Template PARTs Pin
Daniel Weck6-Apr-09 0:56
Daniel Weck6-Apr-09 0:56 
GeneralRe: Error in your code: Template PARTs Pin
Daniel Weck6-Apr-09 0:58
Daniel Weck6-Apr-09 0:58 
GeneralRe: Error in your code: Template PARTs Pin
alrh6-Apr-09 1:55
alrh6-Apr-09 1:55 
GeneralProposal for handling ESC key Pin
Daniel Weck5-Apr-09 23:21
Daniel Weck5-Apr-09 23:21 
GeneralRe: Proposal for handling ESC key Pin
alrh6-Apr-09 1:48
alrh6-Apr-09 1:48 
GeneralRe: Proposal for handling ESC key Pin
Daniel Weck6-Apr-09 8:50
Daniel Weck6-Apr-09 8:50 
GeneralRe: Proposal for handling ESC key Pin
alrh8-Apr-09 22:48
alrh8-Apr-09 22:48 
QuestionCenter Customizable WPF TaskDialog with its parent window [modified] Pin
Roberto Jucá3-Apr-09 11:26
Roberto Jucá3-Apr-09 11:26 
AnswerRe: Center Customizable WPF TaskDialog with its parent window Pin
alrh4-Apr-09 22:33
alrh4-Apr-09 22:33 
GeneralRe: Center Customizable WPF TaskDialog with its parent window [modified] Pin
Roberto Jucá5-Apr-09 8:00
Roberto Jucá5-Apr-09 8:00 
GeneralRe: Center Customizable WPF TaskDialog with its parent window Pin
alrh5-Apr-09 22:56
alrh5-Apr-09 22:56 
GeneralMulti-threaded version Pin
vic_ch200018-Jan-09 3:19
vic_ch200018-Jan-09 3:19 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.