I am trying to make an app in Visual Studio Express 2012 RC. I downloaded some JSON data and deserialized it. But after binding the data to a list box, instead of showing the actual content, it is showing project-name.page-name+Datum in text blocks.
Here is the part of the XAML code:
<ListBox x:Name="listbox1" HorizontalAlignment="Left" Height="687" Margin="10,71,0,0" VerticalAlignment="Top" Width="1346" ItemsSource="{Binding data}">
<TextBlock x:Name="textblock1" TextWrapping="Wrap" Height="50" Width="443" Text="{Binding name}" />
</ListBox>
Here is the part of xaml.cs code
public class RootObject
{
public List<Datum> data { get; set; }
}
public class Datum
{
public string name { get; set; }
}
This is the code which I am using to de-serialize JSON data
stream loading = await load.GetStreamAsync(....);
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
RootObject loaded = (RootObject) ser.ReadObject(loading);
listbox1.DataContext = loaded;
Can anyone tell me what’s wrong my the code? I am using the same thing in a Windows phone app and in that it is working fine except that I used webclient in it.
Please help.
You’ve added a UIElement to the ListBox’s
Itemscollection. The proper way to get the item to display what you want would be to apply an ItemTemplate. You can do it inline or reference a resource.It should work with that setup, though I have no tested it. If this worked, remember to mark it as the answer so others may learn from your inquiry.
Side notes
I’d also suggest scrubbing your sample code of any non-essential properties, for what it’s worth. And avoid hard-coded width/margin values — allow the application elements to size themselves proportionally.