如何使用 System.Runtime.Serialization.Json 通过列表框在 C# XAML//WPF 中



如何显示"Telnet and Service"的整个JSON数组。我已经让它显示机器名称,Ping以及Telnet和服务的第一个数据,但没有显示Telnet和服务的第一个数据。我使用了System.Runtime.Serialization.Json而不是Newtonsoft。

我的 xaml

<Window.DataContext>
<ViewModels:Rootobject/>
</Window.DataContext>

<Grid>
<ListBox x:Name="listboxMachine"                  
HorizontalAlignment="Stretch" Background="Gray" BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding server}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border HorizontalAlignment="Stretch">
<StackPanel >
<Image Width="80" Source="http://fakeimg.pl/100x200/?text=Image_{i}" />
<TextBlock Text="{Binding MachineName}"/>
<TextBlock Text="{Binding Ping}"/>
<TextBlock Text="{Binding Telnet/Description}" />
<TextBlock Text="{Binding Telnet/Port}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>

代码隐藏

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
JsonRead();
}
public void JsonRead()
{
using (Stream stream = File.OpenRead("D:\file.json"))
{
var serializer = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject rootobject = (Rootobject)serializer.ReadObject(stream);
listboxMachine.ItemsSource = rootobject.server;

}
}

}

杰森文件

{  "server": [
{
"MachineName": "Server1",
"Ping": "Server1.company.com",
"Telnet": [
{
"Description": "IIS Application",
"Port": 80
},
{
"Description": "Java Application",
"Port": 8080
}
],
"Service": [
{
"Description": "IIS Service",
"Value": "iisSomething"
},
{
"Description": "Java Service",
"Value": "javaSomething"
}
]
},
{
"MachineName": "Server2",
"Ping": "Server2.company.com",
"Telnet": [
{
"Description": "IIS Application",
"Port": 80
},
{
"Description": "Java Application",
"Port": 8080
}
],
"Service": [
{
"Description": "IIS Service",
"Value": "iisSomething"
},
{
"Description": "Java Service",
"Value": "javaSomething"
}
]
}
] }

服务器.cs

public class Server
{
public string MachineName { get; set; }
public string Ping { get; set; }
public List<Telnet> Telnet { get; set; }
public List<Service> Service { get; set; }
}
public class Telnet
{
public string Description { get; set; }
public int Port { get; set; }
}
public class Service
{
public string Description { get; set; }
public string Value { get; set; }
}

主模型.cs

public class Rootobject
{
public List<Server> server { get; set; }
}

你不能在TextBlock中显示a list of items(Telnet列表(,你需要使用像ListBox这样的ItemsControl

<DataTemplate>
<Border HorizontalAlignment="Stretch">
<StackPanel >
<Image Width="80" Source="http://fakeimg.pl/100x200/?text=Image_{i}" />
<TextBlock Text="{Binding MachineName}"/>
<TextBlock Text="{Binding Ping}"/>
<ListBox ItemsSource="{Binding Path=Telnet}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Margin="5, 0" Content="{Binding Description}" />
<Label Content="{Binding Port}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding Path=Service}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Margin="5, 0" Content="{Binding Description}" />
<Label Content="{Binding Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--<TextBlock Text="{Binding Telnet/Description}" />
<TextBlock Text="{Binding Telnet/Port}" />-->
</StackPanel>
</Border>
</DataTemplate>

最新更新