在 Gridview 内的 Gridview 上显示 Json
Posted
技术标签:
【中文标题】在 Gridview 内的 Gridview 上显示 Json【英文标题】:Display Json on Gridview inside A Gridview 【发布时间】:2022-01-07 16:51:08 【问题描述】:我有一个 Json,如下图所示:
我想在 gridview 内的 gridview 中显示“专业”
XAML:
<GridView x:Name="asesmenGridView">
<GridView.ItemTemplate>
<DataTemplate>
<Grid x:Name="asesmenGrid">
<TextBlock
x:Name="judulT"
Text="Binding Title"/>
<GridView
x:Name="majorGridView"
ItemsSource="Binding DataContext.majordatasource, ElementName=asesmenGridView">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:MajorClass">
<Grid>
<TextBlock
x:Name="kelasT"
Text="x:Bind Major" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
代码:
public MainPage()
this.InitializeComponent();
this.DataContext = this;
public List<MajorClass> majordatasource get; set; = new List<MajorClass>();
string urlPath = "https://.../choose-exam/2";
var httpClient = new HttpClient(new HttpClientHandler());
httpClient.DefaultRequestHeaders.Add("Authorization",
string.Format("Bearer 0", tkn));
var response = await httpClient.GetAsync(urlPath);
string jsonText = await response.Content.ReadAsStringAsync();
try
JsonArray jsonArray = JsonArray.Parse(jsonText);
foreach (JsonValue groupValue in jsonArray)
string title = groupObject["title"].GetString();
JsonArray majorArray = groupObject["majors"].GetArray();
MajorClass majorC = new MajorClass();
foreach (JsonValue majorValue in majorArray)
JsonObject majorObject = majorValue.GetObject();
double majorID = majorObject["major_id"].GetNumber();
string major = majorObject["major"].GetString();
majorC.MajorID = (int)majorID;
majorC.Major = major + ",";
majordatasource.Add(majorC);
Ujian ujian = new Ujian();
ujian.Title = title;
ujiandatasource.Add(ujian);
asesmenGridView.ItemsSource = ujiandatasource;
乌剑类:
public class Ujian
public string Title get; set;
public ObservableCollection<MajorClass> majordatasource get; set;
public Ujian()
Title = string.Empty;
majordatasource = new ObservableCollection<MajorClass>();
public Ujian(string title)
Title = title;
public class MajorClass
public int MajorID get; set;
public string Major get; set;
从上面的代码来看,我并没有设法在gridview中的gridview上显示(检索到的数据只是最后一条数据,重复显示,如下图)。
如何处理?
【问题讨论】:
以下工作是否有效,此线程是否有任何更新? 【参考方案1】:根据你的设计,你需要嵌套列表数据结构,majorGridView
可以直接访问Ujian的majordatasource
属性。请将主要数据源传递给ujian.majordatasource
,但不要访问页面类中单独的主要数据源属性。
例如
Ujian ujian = new Ujian();
ujian.Title = title;
ujian.majordatasource = majordatasource;
ujiandatasource.Add(ujian)
Xaml
<GridView
x:Name="majorGridView"
ItemsSource="Binding majordatasource">
更新
try
JsonArray jsonArray = JsonArray.Parse(jsonText);
foreach (JsonValue groupValue in jsonArray)
var groupObject = groupValue.GetObject();
string title = groupObject["title"].GetString();
JsonArray majorArray = groupObject["majors"].GetArray();
var majordatasource = new ObservableCollection<MajorClass>();
foreach (JsonValue majorValue in majorArray)
MajorClass majorC = new MajorClass();
JsonObject majorObject = majorValue.GetObject();
double majorID = majorObject["major_id"].GetNumber();
string major = majorObject["major"].GetString();
majorC.MajorID = (int)majorID;
majorC.Major = major + ",";
majordatasource.Add(majorC);
Ujian ujian = new Ujian();
ujian.majordatasource = majordatasource;
ujian.Title = title;
ujiandatasource.Add(ujian);
catch
【讨论】:
在ujian.majordatasource = majordatasource;
。有一个错误:Cannot implicitly convert type 'System.Collections.Generic.List<KipinSchool_Win10.PTO.MajorClass>' to 'System.Collections.ObjectModel.ObservableCollection<KipinSchool_Win10.PTO.MajorClass>'
将此行 public List<MajorClass> majordatasource get; set; = new List<MajorClass>();
更新为 public ObservableCollection<MajorClass> majordatasource get; set; = new ObservableCollection<MajorClass>();
我已经像你上面的代码一样改了,但还是有同样的问题,就是最后一次数据被成功检索并重复显示。
你更新过上面的 xaml 代码了吗?
是的,我已经把它从ItemsSource="Binding DataContext.majordatasource, ElementName=asesmenGridView"
改成了ItemsSource="Binding majordatasource"
以上是关于在 Gridview 内的 Gridview 上显示 Json的主要内容,如果未能解决你的问题,请参考以下文章
由 UpdatePanel 内 GridView 内的 LinkButton 触发的完整回发