如何在WPF TreeView中显示JSON [英] How to display JSON in WPF TreeView

查看:498
本文介绍了如何在WPF TreeView中显示JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取JSON,然后将其显示在WPF树视图中.

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

这是代码...

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

还有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>

起点和终点(上面)看起来不错(大概是因为它们包含要显示的子元素).

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

但是Phase元素应该只包含一个值.读取"GROUND"的单个字符串.但是由于某种原因,它被分解为一个charArray.并显示在多个元素中,如上所示.

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?

感谢您的帮助!

推荐答案

问题是您的XAML只能显示字典值中的集合,并且如果存在string,则它将被视为字符集合.一种快速的解决方案是创建一个转换器,该转换器会将您的字符串转换为字符串集合.

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.

为此,您需要一个值转换器(对不起,我用c#编写代码)

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;
    }
}


在资源中实例化此转换器:


Instantiate this converter in resources:

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


并使用它:


and use it:

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

这篇关于如何在WPF TreeView中显示JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆