我需要在运行时删除Grid(grid1)的指定元素。这是我添加元素的代码。删除Grid的指定元素
examControls.Add(excontrol); // add the element on the ArrayList
excontrol.Margin = new Thickness(x, y + margin, 0, 0);
grid1.Children.Add(excontrol);
如何在运行时删除指定的“excontrol”元素(在运行时添加)?
在此先感谢
我需要在运行时删除Grid(grid1)的指定元素。这是我添加元素的代码。删除Grid的指定元素
examControls.Add(excontrol); // add the element on the ArrayList
excontrol.Margin = new Thickness(x, y + margin, 0, 0);
grid1.Children.Add(excontrol);
如何在运行时删除指定的“excontrol”元素(在运行时添加)?
在此先感谢
如果保持控制的记录,你可以简单地做:
grid1.Children.Remove(excontrol);
如果你没有保存你希望你删除将控制变量必须以某种方式识别它(标签,名称),然后在网格的孩子中找到该控件,然后致电Remove
。
谢谢你,非常有帮助,我解决了! – 2013-03-25 17:59:13
是否释放了内存空间? – 2017-07-27 15:25:06
@AricFowler - 我会这样认为的,一旦'excontrol'超出范围,并没有引用其他地方。 – ChrisF 2017-07-27 15:33:54
grid1.Children.Remove(excontrol) //edited per your edit -- this is exactly what ChrisF posted though
或
grid1.Children.RemoveAt(index)
谢谢你,非常有帮助! – 2013-03-25 17:58:25
@NicolòCarpignoli没问题。将来,文档是你的朋友。为了找到你的问题的答案,我只是搜索“c#wpf网格”。作为第一个结果,[弹出](http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx)。你会看到'Children'是一个['UIElementCollection'](http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.aspx),它可以向你显示你正在看什么对于。 – tnw 2013-03-25 18:02:06
现在,当我谷歌它,这个答案弹出。所以,你知道...谢谢大声笑 – 2017-11-19 23:03:32
你需要做的是,在XAML来代替。发布你需要的截图,我们可以给你一个正确的方式来实现它。 – 2013-03-25 17:34:55