Display an Exception in Treeview with innerexception being child
-
I am trying to display an exception in a treeview with each innerexception being a child. Here is what I have so far, but this only displays the exception.tostring() values.
<GridViewColumn Header="Exception" Width="Auto" DisplayMemberBinding=""> <GridViewColumn.CellTemplate> <DataTemplate> <TreeView ItemsSource="{Binding Path=Exception}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type sys:Exception}" ItemsSource="{Binding Path=InnerException}"> <TextBlock Text="{Binding Path=Message}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>
Troy Zajac
-
I am trying to display an exception in a treeview with each innerexception being a child. Here is what I have so far, but this only displays the exception.tostring() values.
<GridViewColumn Header="Exception" Width="Auto" DisplayMemberBinding=""> <GridViewColumn.CellTemplate> <DataTemplate> <TreeView ItemsSource="{Binding Path=Exception}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type sys:Exception}" ItemsSource="{Binding Path=InnerException}"> <TextBlock Text="{Binding Path=Message}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>
Troy Zajac
I think the real problem with this method is that InnerException is not a collection. You may be able to use a converter to convert InnerException to an array of exception objects. However, there is a much easier method. Here is a simple sample Window that does something similar to what I think you want. XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type s:Exception}">
<Expander Margin="20,0,0,0" Header="{Binding Message}" Content="{Binding InnerException}"/>
</DataTemplate>
</Window.Resources>
<DockPanel>
<Button DockPanel.Dock="Bottom" Click="Button_Click">Error!</Button>
<ContentPresenter Name="Bob" />
</DockPanel>
</Window>CODE (Pardon the VB)
Class MainWindow
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Try
a()
Catch ex As Exception
Me.Bob.Content = ex
End Try
End SubPrivate Sub a() Try b() Catch ex As Exception Throw New ArgumentException("WOW!", ex) End Try End Sub Private Sub b() Try c() Catch ex As Exception Throw New InvalidCastException("no good", ex) End Try End Sub Private Sub c() Throw New InvalidOperationException("Bad magic") End Sub
End Class
I use WPF's awesomeness with DataTemplates. Any exception object it displays will look like an Expander.