I’m attempting to parse a JSON file into a ListBox, but I have no idea how to do so and have been having difficulty understanding examples here and elsewhere.
Currently, I have the following:
XAML
<controls:Pivot Title="Episode Guide">
<!--Pivot item one-->
<controls:PivotItem Header="season 1">
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="Season1Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season1Box_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<Image Source="{Binding Path=image1}"/>
<TextBlock Text="{Binding Path=title1}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem Header="season 2">
<Grid x:Name="ContentPanel2" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="Season2Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season2Box_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<Image Source="{Binding Path=image2}"/>
<TextBlock Text="{Binding Path=title2}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
<controls:PivotItem Header="season 3">
<Grid x:Name="ContentPanel3" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="Season3Box" Margin="0,0,0,0" VerticalAlignment="Top" Height="607" SelectionChanged="Season3Box_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<Image Source="{Binding Path=image3}"/>
<TextBlock Text="{Binding Path=title3}" TextWrapping="Wrap" Margin="10,10,0,0" FontSize="25" Foreground="White" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
</controls:Pivot>
C#
private void EpisodeGuideStream()
{
var client = new WebClient();
client.OpenReadCompleted +=
(s, eargs) =>
{
var serializer = new DataContractJsonSerializer(typeof(RootObject));
if (eargs.Error != null)
{
if (eargs.Error.Message.Contains("NotFound"))
{
MessageBox.Show("Could not retrieve episode guide", "Error", MessageBoxButton.OK);
}
else
{
MessageBox.Show("Could not retrieve episode guide", "Error", MessageBoxButton.OK);
}
}
else
{
var episodeGuide = (RootObject)serializer.ReadObject(eargs.Result);
//I have no idea what I'm doing (Part 1)
List<string> episode1 = new List<string>();
List<string> title1 = new List<string>();
List<string> director1 = new List<string>();
List<string> writer1 = new List<string>();
List<string> airdate1 = new List<string>();
List<string> sypnosis1 = new List<string>();
List<string> image1 = new List<string>();
foreach (var obj in episodeGuide.SEASON1)
{
//I have no idea what I'm doing (Part 2)
episode1.Add(obj.EPISODE);
title1.Add(obj.TITLE);
director1.Add(obj.DIRECTOR);
writer1.Add(obj.WRITER);
airdate1.Add(obj.AIRDATE);
sypnosis1.Add(obj.SYPNOSIS);
image1.Add(obj.IMAGE);
// Season1Box.ItemsSource = figure out what goes here;
}
foreach (var obj2 in episodeGuide.SEASON2)
{
// populate Season2Box
}
foreach (var obj3 in episodeGuide.SEASON3)
{
// populate Season3Box
}
}
};
var uri = new Uri(jsonfile);
client.OpenReadAsync(uri);
}
public class SEASON1
{
public string EPISODE { get; set; }
public string TITLE { get; set; }
public string DIRECTOR { get; set; }
public string WRITER { get; set; }
public string AIRDATE { get; set; }
public string SYPNOSIS { get; set; }
public string IMAGE { get; set; }
}
public class SEASON2
{
public string EPISODE { get; set; }
public string TITLE { get; set; }
public string DIRECTOR { get; set; }
public string WRITER { get; set; }
public string AIRDATE { get; set; }
public string SYPNOSIS { get; set; }
public string IMAGE { get; set; }
}
public class SEASON3
{
public string EPISODE { get; set; }
public string TITLE { get; set; }
public string DIRECTOR { get; set; }
public string WRITER { get; set; }
public string AIRDATE { get; set; }
public string SYPNOSIS { get; set; }
public string IMAGE { get; set; }
}
public class RootObject
{
public List<SEASON1> SEASON1 { get; set; }
public List<SEASON2> SEASON2 { get; set; }
public List<SEASON3> SEASON3 { get; set; }
}
private void Season1Box_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//To eventually navigate to another page
}
private void Season2Box_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//To eventually navigate to another page
}
private void Season3Box_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//To eventually navigate to another page
}
The JSON file is parsed correctly and I can retrieve bits of data here and there, but I have no clue how to put them all together into one ListBox.
What I’m attempting to do is to have a ListBox that shows the “IMAGE” from the JSON next to the “TITLE” from the JSON. Clicking them should eventually navigate to a new page with all the other information, I could potentially pass all the bits and pieces through QueryStrings or something once I figure this out.
If someone could check to see that my last bit makes sense and point me in the right direction for the ListBox I’d appreciate it.
You should get rid of the
SEASON2andSEASON3classes, and renameSEASON1toEpisode. The class-names are not used in the serialization. You should also change theIMAGE-property to typeUri, so you can bind it to an image.You can bind (or assign) the list to a ListBox’s
ItemsSourceproperty, and it will get populated with rows based on the objects in the list. Each list item uses their corresponding object as a DataContext, and you can bind to their properties.To bind a control’s attribute to an object property, you can use the syntax
Text="{Binding Path=MyProperty}". You can even address nested propertes, likeMyProperty.OtherProperty.