简体   繁体   English

删除Grid的指定Children元素

[英]Remove specified Children element of a Grid

I need to remove at runtime a specified element of a Grid (grid1). 我需要在运行时删除Grid(grid1)的指定元素。 This is the code where i add the elements. 这是我添加元素的代码。

 examControls.Add(excontrol);  // add the element on the ArrayList
 excontrol.Margin = new Thickness(x, y + margin, 0, 0);
 grid1.Children.Add(excontrol);   

How can i remove at runtime a specified "excontrol" element (added at runtime) ? 如何在运行时删除指定的“excontrol”元素(在运行时添加)?

Thanks in advance 提前致谢

If you keep a record of the control you can simply do: 如果您保留控件记录,您可以执行以下操作:

grid1.Children.Remove(excontrol);

If you don't have a variable that holds the control you wish to remove you'll have to identify it in some way (Tag, Name), then find that control in the grid's children and then call Remove . 如果您没有包含要删除的控件的变量,则必须以某种方式识别它(标记,名称),然后在网格的子项中找到该控件,然后调用Remove

grid1.Children.Remove(excontrol) //edited per your edit -- this is exactly what ChrisF posted though

要么

grid1.Children.RemoveAt(index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM