Adding a grid as an item of a ListBox at runtime e

2019-09-12 06:50发布

问题:

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:

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.



回答2:

The code after fixing the errors. It's a good piece of code for beginners.

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    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();

        foreach (string item in lstDays)
        {
            //Grid definition
            Grid mygrid = new Grid();
            mygrid.Width = 400;
            ColumnDefinition c1 = new ColumnDefinition(); //creating a column
            c1.Width = new GridLength(200);
            mygrid.ColumnDefinitions.Add(c1);

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

            TextBlock txtDays = new TextBlock(); //Creating a TextBlock
            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,0);       //Placing the item in a particular row inside the grid          

            lstBox.Items.Add(mygrid);     //Placing grid inside a listBox    
        }

        RowDefinition rNewRow = new RowDefinition();
        ContentPanel.RowDefinitions.Add(rNewRow);
        ContentPanel.Children.Add(lstBox);
    }
}