wpf 删除网格的指定子元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15621080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:18:38  来源:igfitidea点击:

Remove specified Children element of a Grid

c#wpfgridchildren

提问by Nicolò Carpignoli

I need to remove at runtime a specified element of a Grid (grid1). This is the code where i add the elements.

我需要在运行时删除网格 (grid1) 的指定元素。这是我添加元素的代码。

 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

提前致谢

回答by ChrisF

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.

回答by tnw

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

or

或者

grid1.Children.RemoveAt(index)