Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Display an Exception in Treeview with innerexception being child

Display an Exception in Treeview with innerexception being child

Scheduled Pinned Locked Moved WPF
wpfwcf
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Troyzajac
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • T Troyzajac

      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

      R Offline
      R Offline
      rhuiden
      wrote on last edited by
      #2

      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 Sub

      Private 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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups