如何删除动态创建的按钮 [英] How to delete Dynamic created buttons..
问题描述
在预定义按钮的单击事件说显示动态btns"后,我知道如何向窗口添加动态按钮
I knw how to add dynamic buttons to window after a click event on predefined button say "show dynamic btns"
for(int i=0;i<4;i++)
{Button bz= new Button();
bz.Height = 50;
bz.Width = 50;
Grid gg=new Grid(); // dynamic grid that add bz
gg.Children.Add(bz);
ThumGrid.Children.Add(gg);//predefined Grid
}
这将创建4个动态网格和按钮.现在我想删除所有这些. bz和gg..pls告诉我我该怎么做?..
this will create 4 dynamic grid and buttons. now i want to remove all of them eg. bz and gg..pls tell me how can i do that?..
推荐答案
请注意,网格的子元素只是另一个集合.Add
和Remove
方法的工作原理与任何集合一样.有趣的是,Remove需要一个UIElement,因此如果您使用foreach遍历该集合,则会得到一个对象,该对象需要转换为UIElement才能删除.Clear
方法显然是最好的选择,但是您可以使用Remove
方法
Note that the children of the grid, is just another collection. TheAdd
andRemove
methods work just like any collection. Interesting thing is that the Remove needs a UIElement, so if you go through the collection with a foreach, you get an object, which needs to be converted to a UIElement in order to remove. TheClear
method is obviously your best choise, but you can do a selective remove using theRemove
method
进行选择性删除,几乎所有具有Add
的事物,也具有Remove
.要确保什么?请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.controls.grid.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.children.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.controls.uielementcollection.aspx [ ^ ].
现在,您阅读文档的能力出了什么问题?也许您还需要一个链接:
Microsoft Q209354 .
—SA
Nearly everything that hasAdd
, also hasRemove
. What to make sure? Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.children.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection.aspx[^].
Now, what''s wrong with your ability to read the documentation? Perhaps you need one more link:
Microsoft Q209354.
—SA
您需要保留对Grid
的引用,直到要删除它们为止. (Grid
保留对Button
s的引用.)
对于您的示例,在窗口类中创建一个私有字段:
You need to keep references to theGrid
s until you want to delete them. (TheGrid
s keep the references to theButton
s.)
For your example, create a private field in the window class:
private List<Grid> DynamicGrids = new List<Grid>();
然后添加:
Then add:
DynamicGrids.Add(gg);
到创建Grid
s和Button
s的for
循环.
然后,当您要删除它们时:
to the for
loop that creates the Grid
s and Button
s.
Then when you want to remove them:
foreach (Grid g in DynamicGrids)
{
ThumGrid.Children.Remove(g);
}
假设ThumGrid
包含您不想删除的其他元素,否则就很简单:
This assumes that ThumGrid
contains other elements that you don''t want to remove, otherwise it is simply:
ThumGrid.Children.Clear();
在使用IEnumerator
(例如,foreach
)遍历该集合时,请注意不要从ThumGrid.Children
中删除元素,否则会抛出异常:System.InvalidOperationException
.
Be careful not to Remove elements from ThumGrid.Children
while iterating over that collection with an IEnumerator
(e.g., foreach
) or else you will throw an exception: System.InvalidOperationException
.
这篇关于如何删除动态创建的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!