从网格控件中删除特定类型的子项 [英] Remove Specific Type of Children from Grid Control

查看:22
本文介绍了从网格控件中删除特定类型的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网格控件中删除一个类型为边框的假设控件.我如何在 WPF C# 中实现它?

I want to remove a suppose control whose type is of border, from the grid control. how can I achieve it in WPF C# ?

抱歉,各位,我的问题是我有在 XAML 端有 GUI 设计的网格控件和使用 C# 添加的用户控件,并且一些控件是重叠的.删除了一些控件,但保留了一些控件,它们彼此重叠.如何删除所有控件.您发布的代码适用于未重叠的控件,但对于重叠的控件,它不起作用.

Sorry Guys, My problem is that I have grid control which has GUI design at XAML end and user control which are added using C# and the some controls are overlapped. Some controls are removed but some are left which overlap one another. How can I remove all controls. The code you have posted work for control which are not overlapped but for overlapped ones, it didn't work.

推荐答案

这是我的代码:

 int intTotalChildren = grdGrid.Children.Count-1;
            for (int intCounter = intTotalChildren; intCounter > 0; intCounter--)
            {
                if (grdGrid.Children[intCounter].GetType() == typeof(Border))
                {
                    Border ucCurrentChild = (Border)grdGrid.Children[intCounter];
                    grdGrid.Children.Remove(ucCurrentChild);
                }                
            }

我的错误是每次我在 for 循环中使用 Children.Count 并且每次我删除一个孩子时,Children.Count 已更改,但并非所有子项都已删除.

My error was that each time I used the Children.Count in the for loop and every time I removed a child, the Children.Count changed and not all children were removed.

这篇关于从网格控件中删除特定类型的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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