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 5977515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:22:54+00:00 2026-05-22T21:22:54+00:00

I have a bit of code that reads a json response from an HTTP

  • 0

I have a bit of code that reads a json response from an HTTP server, it then parses this and inserts the data into a ListBox control.

The event I fire off when the download is complete is the following:

 void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
 {
     DataContractJsonSerializer ser = null;

     try
     {
         ser =
        new DataContractJsonSerializer(typeof(ObservableCollection<UserLeaderboards>));

         ObservableCollection<UserLeaderboards> users =
            ser.ReadObject(e.Result) as ObservableCollection<UserLeaderboards>;

         foreach (UserLeaderboards em in users)
         {
             int Fid = em.id;
             string Fusername = em.username;
             int Fscore = em.score;
             lstbLeaders.Items.Add(Fid + Fusername + Fscore);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }

Now, when I do the items.add I presume it’s just joining up the 3 variables and adding it to one column in the ListBox. This works fine and I see all 3 items joined up and displayed.

I want to separate this and make it look a bit nicer so I’ve created some XAML to try and bind the variables to textblocks. The following is just binding the username. I also have a public class that get/sets all 3 variables.

<ListBox Height="346" HorizontalAlignment="Left" Margin="5,221,0,0" 
         Name="lstbLeaders" VerticalAlignment="Top" Width="446">
   <DataTemplate>                            
       <TextBlock Text="{Binding Source=Fusername}" />                           
   </DataTemplate>
</ListBox>

When running the above I get nothing displayed at all. I have a feeling it’s something simple?

Thanks.

  • 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-22T21:22:55+00:00Added an answer on May 22, 2026 at 9:22 pm

    To display a simple string your xaml should look like this:

    <ListBox Height="346" HorizontalAlignment="Left" Margin="5,221,0,0" 
             Name="lstbLeaders" VerticalAlignment="Top" Width="446">
        <ListBox.ItemTemplate>
            <DataTemplate>                            
               <TextBlock Text="{Binding}" />                           
            </DataTemplate>
        <ListBox.ItemTemplate>
    </ListBox>
    

    and you have to provide an object instead of a simple string if you want to split the properties to make it look nicer. If you just add Fid + Fusername + Fscore you will end up with a plain string.

    <ListBox Height="346" HorizontalAlignment="Left" Margin="5,221,0,0" 
             Name="lstbLeaders" VerticalAlignment="Top" Width="446">
        <ListBox.ItemTemplate>
            <DataTemplate>                            
               <StackPanel Orientation="Horizontal">
                   <TextBlock Text="{Binding Id}" />                           
                   <TextBlock Text="{Binding Name}" />                           
                   <TextBlock Text="{Binding Score}" />                           
               </StackPanel> 
            </DataTemplate>
        <ListBox.ItemTemplate>
    </ListBox>
    

    You will need a view class:

    public class UserView
    {
        public string Id {get;set;}
        public string Name {get;set;}
        public int Score {get;set;}
    }
    

    in your code behind:

    var usersList = new List<UserView>();
    
    foreach (UserLeaderboards em in users)
    {
        int Fid = em.id;
        string Fusername = em.username;
        int Fscore = em.score;
        usersList.Add(new UserView { Id = Fid, Name = Fusername, Score = Fscore} );
    }
    
    lstbLeaders.ItemsSource = usersList;
    

    Further notes:

    • Why not bind the ObservableCollection<UserLeaderboards> direcectly to the list box?

    If there is no reason to convert to an other type skip the foreach part of the code and simply set lstbLeaders.ItemsSource = users;.

    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        try
        {
             var ser = new DataContractJsonSerializer(
                        typeof(ObservableCollection<UserLeaderboards>));
    
             var users = ser.ReadObject(e.Result)
                             as ObservableCollection<UserLeaderboards>;
    
             lstbLeaders.ItemsSource = users;
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
    }
    
    • Take look at the MVVM pattern. If you want to work with XAML you should know about this. It simplifies your work and creates cleaner code.

    • If you want to add edit functionality or data can change you may need to implement INotifyPropertyChanged on the View class.

    • You can use type inference which especially helps when working with cumbersome class names. var list = new ObservableCollection<SomeLongTypeName>() saves much typing and screen estate.

    • Hungarian notation makes me cringe 😉

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

Sidebar

Related Questions

I have the following bit of code that reads data from the an Oracle
I have a bit of code that basically reads an XML document using the
I have a bit of code that looks like this: text = reg.Replace(text, new
Well, I have this bit of code that is slowing down the program hugely
I have application reads in data from text file. Recently I realized that I
I have an small ASP.NET application that reads data from a table and sends
i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have a bit of code that passes around a ton of objects and
I have a bit of Javascript code that creates a save friendly version of
I have the following bit of code that worked as expected before we upgraded

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.