从Grid.Children中移除特定的对象实例? [英] Remove specific object instance from Grid.Children?
问题描述
我有一个 List< T>
,其中一些 UserControl
s。
在主窗口中有一个 Grid
,一些 UserControl
会被添加到 Grid.Children
。
现在我希望能够从这个 Grid
中删除特定的 UserControl
。我想做这样的事情
layoutRoot.Children.Remove(controlList [1]);
这可能吗?
我只知道 FindName()
和 FindResource()
,但所有 UserControl
s没有名称,所以我不能使用这些方法:($ / b>
在此先感谢!
如果你知道用户控件的类型,那么你可以使用像这样的方法:
static T FindVisualChild< T>(Visual parent)其中T:Visual
{
T child = default(T);
$ num_Visuals = VisualTreeHelper.GetChildrenCount(parent);
for(int i = 0; i< numVisuals; i ++)
{
var visual =(Visual)VisualTreeHelper.GetChild(parent ,i);
child = visual as T;
if(child == null)
child = FindVisualChild< T>(visual);
if(child != null)
break;
}
return child;
}
I have a List<T>
with some UserControl
s.
On the main window there is a Grid
and some of the UserControl
s will be added to Grid.Children
.
Now I would like to be able to remove specific UserControl
s from this Grid
e.g. I would like to do something like this
layoutRoot.Children.Remove(controlList[1]);
Is this possible?
I only know FindName()
and FindResource()
but all the UserControl
s don't have names so that I can't use these methods :(
Thanks in advance!
just an idea to get you started, if you know the type of your user control, you can use methods like this:
static T FindVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
var visual = (Visual)VisualTreeHelper.GetChild(parent, i);
child = visual as T;
if (child == null)
child = FindVisualChild<T>(visual);
if (child != null)
break;
}
return child;
}
这篇关于从Grid.Children中移除特定的对象实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!