在运行时将一个网格添加为ListBox的一个项目,最终会出现未知错误/崩溃 [英] Adding a grid as an item of a ListBox at runtime ends up with an unknown error/crash
问题描述
- 在Grid(mygrid)中放置一个TextBlock(txtDays)(作为行)
- 将网格放置在ListBox(lsBox)中
- 最后将ListBox放置到另一个Grid(ContentPanel)中
该程序仅在没有正确错误的情况下崩溃,并且执行此代码时模拟器会停止。如果我把Grid(在2.)放入一个stackPanel或直接放入另一个Grid(在3.)而不放入列表框中,代码就可以工作,但不能与ListBox一起工作。
任何意见和帮助表示感谢。
谢谢。
private void PhoneApplicationPage_Loaded(object sender,RoutedEventArgs e)
{
string [] lstDays = new string [] {Monday,Tuesday , 周三周四周五周六周日 };
//创建一个ListBox
ListBox lstBox = new ListBox();
//网格定义
网格mygrid = new Grid();
ColumnDefinition c1 = new ColumnDefinition(); //创建一个列
mygrid.ColumnDefinitions.Add(c1);
int i = -1;
foreach(lstDays中的字符串项)
{
i + = 1;
TextBlock txtDays = new TextBlock(); //创建一个TextBlock
RowDefinition r1 = new RowDefinition(); //创建一行
mygrid.RowDefinitions.Add(r1);
txtDays.Text = item;
txtDays.TextAlignment = TextAlignment.Left;
if(item ==Thursday)
{
txtDays.TextAlignment = TextAlignment.Right;
txtDays.Foreground =新的SolidColorBrush(Colors.Green);
}
mygrid.Children.Add(txtDays); //将TextBlock添加到网格
Grid.SetRow(txtDays,i); //将项目放置在网格内的特定行中
lstBox.Items.Add(mygrid); //将网格放置在listBox中(此处为ERROR)
RowDefinition rNewRow = new RowDefinition();
ContentPanel.RowDefinitions.Add(rNewRow);
ContentPanel.Children.Add(lstBox);
}
mygrid
到 lstBox
对每个 foreach
迭代。在循环外部添加 lstBox.Items.Add(mygrid);
,以便仅添加一次。
或者将 mygrid
的定义放在循环中,如果你想要多个网格。
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);
}
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.
这篇关于在运行时将一个网格添加为ListBox的一个项目,最终会出现未知错误/崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!