This project has moved. For the latest updates, please go here.

Using DataTemplate in TaskDialog.ExpansionDataContent

Sep 28, 2007 at 5:05 AM
Edited Sep 28, 2007 at 5:10 AM
I finally figured out the answer to my previous post http://www.codeplex.com/wpfcontrib/Thread/View.aspx?ThreadId=15714 ...

Create a simple object:
public class ErrorInfo {

private string formattedText;
public string FormattedText {
get { return formattedText; }
set {
formattedText = value;
}
}
}

Create a Data Template
<DataTemplate x:Key="MyDataTemplate">
<DockPanel>
<Border BorderThickness="5" CornerRadius="5" BorderBrush="Green" Margin="5">
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Visible" Height="200" ScrollViewer.HorizontalScrollBarVisibility="Visible" Content="{Binding Path=FormattedText}"></ScrollViewer>
</Border>
</DockPanel>
</DataTemplate>

Create the dialog in C# and show it
TaskDialog dialog = new TaskDialog();
dialog.Name = "ErrorDialog";
dialog.Title = "Title";
dialog.Header = "An unhandled exception has occurred, please contact support";
dialog.Owner = this;
dialog.StandardButtons = TaskDialogButtons.OK | TaskDialogButtons.Cancel;
dialog.MainIcon = TaskDialogIconConverter.ConvertFrom(TaskDialogIcon.Error);

dialog.ExpansionPosition = TaskDialogExpansionPosition.Header;
dialog.Width = 600;

DataTemplate template = dialog.FindResource("MyDataTemplate") as DataTemplate; // fetch data template from resource file
ErrorInfo info = new ErrorInfo();
info.FormattedText = formatter.Writer.ToString();
dialog.ExpansionContent = info; // set the object to be the data source
dialog.ExpansionContentTemplate = template; // set the data template

dialog.Show();

TaskDialogResult result = dialog.Result;
if (result.StandardButton == TaskDialogButtons.Cancel)
App.Current.MainWindow.Close();