How to display JSON in WPF TreeView


I am reading in JSON and then displaying it in a WPF treeview.

Here is the code...

Class MainWindow Public Sub New() InitializeComponent() Dim dic = GetThreadedObject(GetJASN())("phases") Dim items = dic(0) tView.ItemsSource = items End Sub Private Function GetJASN() As String Dim output As String = My.Computer.FileSystem.ReadAllText(My.Application.Info.DirectoryPath & "\UAL525 Phase of Flight.json") Return output End Function Private Function GetThreadedObject(JASN As String) Dim Js As New JavaScriptSerializer() Js.MaxJsonLength = JASN.Length * 2 Dim j = Js.Deserialize(Of Object)(JASN) Return j End Function End Class

And the WPF...

<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <TreeView x:Name="tView"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Value}" > <HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" Foreground="Red"/> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> <TextBlock Text="{Binding Key}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </Grid>

<a href="https://i.stack.imgur.com/X0bxH.png" rel="nofollow"><img alt="Start Point and End Point" class="b-lazy" data-src="https://i.stack.imgur.com/X0bxH.png" data-original="https://i.stack.imgur.com/X0bxH.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Start and End points (above) look fine (presumably because they contain child elements to display).

<a href="https://i.stack.imgur.com/izoMU.png" rel="nofollow"><img alt="Phase" class="b-lazy" data-src="https://i.stack.imgur.com/izoMU.png" data-original="https://i.stack.imgur.com/izoMU.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

But the Phase element should just contain one value. A single string that reads "GROUND". But it is broken up into a charArray for some reason. And displayed in multiple elements as shown above.

So what is the key to fixing this? Multiple data templates that display a string differently from other objects?

Thanks for any help!


The problem is that your XAML can only show a collections in dictionary's value and if there is a string, then it will be considered as collection of characters. One of the quick sollutions is to create a converter, which will transform your strings into string collections.

For this you need a value converter(sorry I do code in c#)

public class ValConv : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string str) { return new List<string> { str }; } return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } }

<br /> Instantiate this converter in resources:

<Window.Resources> <local:ValConv x:key="valKonv"/> </Window.Resources>

<br /> and use it:

<HierarchicalDataTemplate ItemsSource="{Binding Value, Converter={StaticResource valConv}}" >


Here is the code Rekshino submitted, in Vb.

Imports System.Globalization Public Class ValConv Implements IValueConverter Private Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert If (TypeOf value Is String) Then Dim newStr As New List(Of String) newStr.Add(value) Return newStr Else Return value End If End Function Private Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack Return value End Function

End Class

<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp2" Title="Window1" Height="300" Width="300"> <Window.Resources> <local:ValConv x:Key="valConv"/> </Window.Resources> <Grid> <TreeView x:Name="tView"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Value, Converter={StaticResource valConv}}" > <HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" Foreground="Red"/> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> <TextBlock Text="{Binding Key}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </Grid>


  • Clr namespace mapping to default xaml namespaces
  • How to have a QTextBrowser to display contents of a QTextEdit?
  • Styling ListBoxItem's of nested ListBoxes [WPF]
  • How to fetch the entry text on a different window?
  • How to align text in a WPF menu item?
  • WPF MVVM WinformsHost with OpenGL-Control
  • QStandardItemModel::removeRows() does not work in my use case
  • change volume of mp3 playing via wmplib in c#
  • Powerpoint 2010 VSTO AddIn taskpane on multiple Windows
  • How to clip image to ellipse in XAML
  • Keep transition effect on 1 div from moving the div that follows?
  • Updating one element of a bound Observable collection
  • How to convert WPF project so it can be used as a class library by a separate exe
  • Android Lock Screen C# .NET Replica
  • XSD with multi occurrences unordered
  • Running jasmine tests for a component with NgZone dependency
  • How to resolve permission denied maybe missing internet permission?
  • Splash Screen will not display
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • WPF Template Binding in ToggleButton UserControl
  • WP8.1 AppBarButton holding event
  • Private IP address in reserved subnet range
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • JBoss External Properties Files in Classpath
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Alert pop up with LWUIT
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Pass value from viewmodel to script in zk
  • Display issues when we change from one jquery mobile page to another in firefox
  • To display the title for the current loaction in map in iphone
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • AngularJs get employee from factory
  • trying to dynamically update Highchart column chart but series undefined
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy