In the code below, I am trying to ...
- Place a TextBlock(txtDays) inside the Grid(mygrid) (as row)
- Place the Grid inside a ListBox(lsBox)
- 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);
}