Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6975249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:20:38+00:00 2026-05-27T17:20:38+00:00

I am relatively new to C#, silverlight and the whole data binding paradigm. I

  • 0

I am relatively new to C#, silverlight and the whole data binding paradigm. I have been working on a little test app that pulls data from reddit via their API using Json.Net. Anyways, I get the data into my application just fine, but now I am having trouble with pushing the data into the UI. I have tried several different configurations to no avail. Anyways, the code is here:

public partial class MainPage : PhoneApplicationPage
{
    string json = "";
    RootObject topic { get; set; }
    public MainPage()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "Retrieving...";
        string url = @"http://www.reddit.com/r/all.json";
        HttpWebRequest hWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        hWebRequest.Method = "GET";
        hWebRequest.BeginGetResponse(Response_Completed, hWebRequest);
    }
    public void Response_Completed(IAsyncResult result)
    {
        HttpWebRequest request = (HttpWebRequest)result.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            json = streamReader.ReadToEnd();
            topic = JsonConvert.DeserializeObject<RootObject>(json);
        }
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            this.DataContext = topic.data.children[0].data.title;
            textBlock1.Text = "Done.";
        });
    }

That is the main part of my code. The remaining classes are here, and they are for the deserialization of the JSON the reddit API provides.

public class MediaEmbed
    {
        public string content { get; set; }
        public int? width { get; set; }
        public bool? scrolling { get; set; }
        public int? height { get; set; }
    }
    public class Oembed
    {
        public string provider_url { get; set; }
        public string description { get; set; }
        public string title { get; set; }
        public string url { get; set; }
        public string author_name { get; set; }
        public int height { get; set; }
        public int width { get; set; }
        public string html { get; set; }
        public int thumbnail_width { get; set; }
        public string version { get; set; }
        public string provider_name { get; set; }
        public string thumbnail_url { get; set; }
        public string type { get; set; }
        public int thumbnail_height { get; set; }
        public string author_url { get; set; }
    }
    public class Media
    {
        public string type { get; set; }
        public Oembed oembed { get; set; }
    }
    public class Data2
    {
        public string domain { get; set; }
        public MediaEmbed media_embed { get; set; }
        public object levenshtein { get; set; }
        public string subreddit { get; set; }
        public string selftext_html { get; set; }
        public string selftext { get; set; }
        public object likes { get; set; }
        public bool saved { get; set; }
        public string id { get; set; }
        public bool clicked { get; set; }
        public string title { get; set; }
        public Media media { get; set; }
        public int score { get; set; }
        public bool over_18 { get; set; }
        public bool hidden { get; set; }
        public string thumbnail { get; set; }
        public string subreddit_id { get; set; }
        public string author_flair_css_class { get; set; }
        public int downs { get; set; }
        public bool is_self { get; set; }
        public string permalink { get; set; }
        public string name { get; set; }
        public double created { get; set; }
        public string url { get; set; }
        public string author_flair_text { get; set; }
        public string author { get; set; }
        public double created_utc { get; set; }
        public int num_comments { get; set; }
        public int ups { get; set; }
    }
    public class Child
    {
        public string kind { get; set; }
        public Data2 data { get; set; }
    }
    public class Data
    {
        public string modhash { get; set; }
        public Child[] children { get; set; }
        public string after { get; set; }
        public object before { get; set; }
    }
    public class RootObject
    {
        public string kind { get; set; }
        public Data data { get; set; }
    }       
}

Let’s say that the XAML UI stuff looks like this

            <ListBox>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock x:Name="TitleInfo" />
                        <TextBlock x:Name="AuthorInfo" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

The titles are contained inside of the instance of RootObject called topic. So the way to grab the titles would be

topic.data.children[0].data.title;

However, I have almost no idea how I can bind that to these text boxes or a listbox..I know a datacontext must be set and these items can be binded via code as opposed to xaml, but I can’t figure out any elegant way to do this. Any help? Thanks a ton.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:20:38+00:00Added an answer on May 27, 2026 at 5:20 pm

    You’re almost there, from the looks of things. Your issue is that you are trying to set the DataContext of the whole page to be a single title from one record (this.DataContext = topic.data.children[0].data.title; ) – that’s probably not what you mean to do…

    To get the data into your ListBox, you have 2 options – you can explicitly set the ItemsSource of the ListBox like so

    myListBox.ItemsSource = topic.data.children;
    

    and then update the XAML to show the data from that point in the object graph…

    <ListBox Name="MyListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock x:Name="TitleInfo" Text="{Binding data.title}" />
                    <TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    However, if you are looking to use data elsewhere in the page, you will probably want to set the DataContext for the whole page.

    DataContext = topic;  
    

    and set your XAML on the ListBox to something slightly different…

    <ListBox Name="MyListBox" ItemsSource="{Binding data.children}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock x:Name="TitleInfo" Text="{Binding data.title}" />
                    <TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class in my Silverlight app that (de-)serializes JSON strings to/from an
I have been working with a Linq query in a Silverlight application which returns
I'm relatively new to Nant, what i'd like to do is have a task
I'm new to Silverlight programming, and the DependencyProperty is still relatively new to me,
I'm working on a Silverlight application and I have a hard time setting the
I'm working on a silverlight application that allows users to fill out common forms.
Relatively new to JQuery. I've got some code that does a banner swap with
Relatively new to python. I recently posted a question in regards to validating that
I am relatively new to Silverlight development and I am trying to figure out
I'm new to Silverlight and I wanted to accomplish a relatively simple task:    Create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.