删除Grid的指定Children元素

Nic*_*oli 4 c# wpf grid children

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

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

如何在运行时删除指定的"excontrol"元素(在运行时添加)?

提前致谢

Chr*_*isF 10

如果您保留控件记录,则可以执行以下操作:

grid1.Children.Remove(excontrol);
Run Code Online (Sandbox Code Playgroud)

如果您没有包含要删除的控件的变量,则必须以某种方式识别它(标记,名称),然后在网格的子项中找到该控件然后调用Remove.


tnw*_*tnw 6

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

要么

grid1.Children.RemoveAt(index)
Run Code Online (Sandbox Code Playgroud)