【问题标题】:Remove specific object instance from Grid.Children?从 Grid.Children 中删除特定对象实例?
【发布时间】:2012-10-13 23:37:46
【问题描述】:

我有一个List<T> 和一些UserControls。 在主窗口上有一个Grid,一些UserControls 将被添加到Grid.Children。 现在我希望能够从这个Grid 中删除特定的UserControls,例如我想做这样的事情

layoutRoot.Children.Remove(controlList[1]);

这可能吗? 我只知道FindName()FindResource(),但是所有UserControls 都没有名字,所以我不能使用这些方法:(

提前致谢!

【问题讨论】:

  • 是的,我通过传递UserControlName 值尝试了FindName(),但每个UserControl 的值都是“”。也许我做错了什么。不知道...
  • 我的意思是layoutRoot.Children.Remove(controlList[1]);
  • -.-' 是的,谢谢它有效 xD 我有点注意力不集中,因为它很晚:)

标签: c# wpf grid


【解决方案1】:

只是一个让你开始的想法,如果你知道你的用户控件的类型,你可以使用这样的方法:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多