从Grid.Children中移除特定的对象实例? [英] Remove specific object instance from Grid.Children?

查看:319
本文介绍了从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 UserControls. On the main window there is a Grid and some of the UserControls will be added to Grid.Children. Now I would like to be able to remove specific UserControls 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 UserControls 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆