在下面的代码中,我试图...
- 在 Grid(mygrid) 内放置一个 TextBlock(txtDays) (作为行)
- 将 Grid 放在 ListBox(lsBox) 内
- 最后将 ListBox 放入另一个 Grid(ContentPanel)
该程序只是在没有适当错误的情况下崩溃,并且当我执行此代码时模拟器停止。如果我将 Grid(在 2.中)放置在 stackPanel 中或直接放入另一个 Grid(在 3.中)而不将其放置在 ListBox 中,则代码有效,但不适用于 ListBox。
任何意见,帮助表示赞赏。
谢谢你。
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);
}