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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:10:56+00:00 2026-06-05T22:10:56+00:00

In the code below, I am trying to … Place a TextBlock(txtDays) inside the

  • 0

In the code below, I am trying to …

  1. Place a TextBlock(txtDays) inside the Grid(mygrid) (as row)
  2. Place the Grid inside a ListBox(lsBox)
  3. Finally placing the ListBox into another Grid(ContentPanel)

The program is just crashing without a proper error, and the emulator stops when I execute this code. If I place the Grid (in 2.) inside a stackPanel or directly into the other Grid (in 3.) without placing it into the ListBox, the code works, but not with a ListBox.

Any comments, assistance is appreciated.

Thank you.

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        string[] lstDays = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        //Creating a ListBox
        ListBox lstBox = new ListBox();
        //Grid definition
        Grid mygrid = new Grid();
        ColumnDefinition c1 = new ColumnDefinition(); //creating a column
        mygrid.ColumnDefinitions.Add(c1);

        int i = -1;
        foreach (string item in lstDays)
        {
            i += 1;
            TextBlock txtDays = new TextBlock(); //Creating a TextBlock

            RowDefinition r1 = new RowDefinition(); //Creating a row
            mygrid.RowDefinitions.Add(r1);

            txtDays.Text = item;
            txtDays.TextAlignment = TextAlignment.Left;
            if (item == "Thursday")
            {
                txtDays.TextAlignment = TextAlignment.Right;
                txtDays.Foreground = new SolidColorBrush(Colors.Green);
            }

            mygrid.Children.Add(txtDays); //Adding the TextBlock into the grid
            Grid.SetRow(txtDays,i);       //Placing the item in a particular row inside the grid          
            lstBox.Items.Add(mygrid);     //Placing grid inside a listBox (ERROR here)

        }

        RowDefinition rNewRow = new RowDefinition();
        ContentPanel.RowDefinitions.Add(rNewRow);
        ContentPanel.Children.Add(lstBox);
    }
  • 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-06-05T22:10:57+00:00Added an answer on June 5, 2026 at 10:10 pm

    You’re trying to add mygrid to lstBox on each foreach iteration. Bring lstBox.Items.Add(mygrid); outside the loop so it is added only once.

    Either that or put the definition for mygrid inside the loop if you want multiple grids.

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

Sidebar

Related Questions

Greetings everyone- In my code below I'm trying to add a Row from an
In the code below i`m trying to read a list of selected projects and
In the code below I am trying to clone a git repository to another
Afternoon all, Using the code below I'm trying to load what is render by
The code below is what I am trying to use in order to get
I am trying to run the code below but it keeps locking up my
I am trying to port the code below to mootools 1.1 code but am
I am trying to modify the sample code below. It currently populates a View
The code below is (hopefully) a minimized testcase of my original problem. I'm trying
Note my code below. I am trying to figure out why my data is

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.