如何在c# windows phone 8.1中删除网格中的一行
本文关键字:网格 一行 删除 windows phone | 更新日期: 2023-09-27 18:09:36
我已经开始开发一个windows phone应用程序,我正在动态地创建网格中的行。在某些情况下,我需要删除该行及其所有内容。
下面是我的示例代码。
result = e.Parameter as string;
string[] acst = result.Split('|');
int j = 0;
root2.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(10) });
for (int i = 6; i < acst.Length - 1; i++)
{
TextBlock mytextblock = new TextBlock();
mytextblock.Name = "txtDetails" + root2.Children.Count + 1;
mytextblock.Text = acst[i + 1];
if (mytextblock.Text != "")
{
if (mytextblock.Text.Trim() != "0.00") // if result is 0.00 then i have to delete all the content in that row.
{
if (j == 0)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.HorizontalAlignment = HorizontalAlignment.Stretch;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 1);
Grid.SetRow(mytextblock, (i) / 6);
j++;
}
else if (j == 1)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.Visibility = Visibility.Collapsed;
mytextblock.HorizontalAlignment = HorizontalAlignment.Center;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 2);
Grid.SetRow(mytextblock, (i) / 6);
j++;
}
else if (j == 2)
{
mytextblock.FontSize = 14;
mytextblock.IsTextScaleFactorEnabled = false;
mytextblock.TextWrapping = TextWrapping.Wrap;
mytextblock.HorizontalAlignment = HorizontalAlignment.Left;
mytextblock.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(mytextblock, 3);
Grid.SetRow(mytextblock, (i) / 6);
j=0;
root2.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(60) });
}
}
root2.Children.Add(mytextblock);
}
else
{
root2.RowDefinitions.RemoveAt((i / 6)); // here I'm getting Arugument Exception
}
}
例如,如果我得到mytextblock。第三列中的Text = 0.00(在本例中j=2)。我需要删除第1列和第2列中的内容或删除THST特定行。
我试过"root2.RowDefinitions"。RemoveAt"但我得到了参数异常。我错过了什么?
最初您创建的网格有1行,其索引为0。但是当你执行的时候root2.RowDefinitions.RemoveAt((i / 6));
实际上你得到了root2.RowDefinitions.RemoveAt(1);
所以你应该使用root2.RowDefinitions.RemoveAt((i / 6)-1);
将解决异常
要回答第二个问题,代码太混乱了。我列出了一些
<>之前1:你为什么从6点开始循环2:为什么要除以1/6你到底从结果中得到了什么