Aditya Patil

Aditya Patil

  • NA
  • 535
  • 130.9k

Binding the JSON data to the ListBox without New class

Jul 28 2014 12:21 AM
 I want to bind all above data to the Listbox, there are total 30 details are there I want to show it in the Listbox one after another. How to do that ? at comment I written same comment.
I don;t want to create new Class of the list items, without creating new class for listitems I want to bind it to ListBox control.

Using new class I am successful in binding the ListBox items but withour new Class I can't. Please help me.

My Xaml COde is:
<Grid x:Name="ContentPanel" Grid.Row="1">
<!--<ListBox HorizontalAlignment="Left" Height="535" VerticalAlignment="Top" Width="456" Name="TaskListBox" FontSize="30"/>-->
<ListBox Name="Updates" ItemsSource="{Binding obj2}" HorizontalAlignment="Left" Height="624" VerticalAlignment="Top" Width="456" Margin="0,0,0,0" FontSize="22" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="title" TextWrapping="Wrap" FontWeight="Bold" Height="75" Visibility="Collapsed"/>
<TextBlock x:Name="AIO" TextWrapping="Wrap" Height="604" />
<TextBlock x:Name="magnitude" TextWrapping="Wrap" Height="75" Visibility="Collapsed" />
<TextBlock x:Name="location" TextWrapping="Wrap" Height="75" Visibility="Collapsed" />
<TextBlock x:Name="depth" TextWrapping="Wrap" Height="75" Visibility="Collapsed"/>
<TextBlock x:Name="latitude" TextWrapping="Wrap" Height="75" Visibility="Collapsed"/>
<TextBlock x:Name="longitude" TextWrapping="Wrap" Height="75" Visibility="Collapsed"/>
<TextBlock x:Name="date_time" TextWrapping="Wrap" Height="75" Visibility="Collapsed"/>
<TextBlock x:Name="link" TextWrapping="Wrap" Height="75" Visibility="Collapsed"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ProgressBar Name="ProgressBarRequest" IsIndeterminate="True" Visibility="Collapsed"></ProgressBar>
</Grid>
C# Code is:
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
//string json = @"{ CPU: 'Intel', Drives: [ 'DVD read/writer', '500 gigabyte hard drive' ] }";
String s = Convert.ToString(e.Result);
//JArray a=
JArray obj = JsonConvert.DeserializeObject<JArray>(s);
//JObject obj = JsonConvert.DeserializeObject<JObject>(s);
Debug.WriteLine(obj.ToString());


for(int i=0;i<obj.Count;i++)
{

string jdata = obj[i].ToString();
//MessageBox.Show(jdata);
JObject obj2 = JObject.Parse(jdata);


string t = (string)obj2["title"];
string m = (string)obj2["magnitude"];
string loc = (string)obj2["location"];
string d = (string)obj2["depth"];
string lat = (string)obj2["latitude"];
string lon = (string)obj2["longitude"];
string dt = (string)obj2["date_time"];
string l = (string)obj2["link"];
//Here I want to bind all above data to the Listbox, there are total 30 details are there I want to show it in the Listbox one after another. How to do that ?
}

}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
}
}
Let me know How I can bind all above strings to the textblocks those are inside Listbox, so in a single page I can view all 30 details about the Data, which I fetched. Urgent help please.ho