如何清除 WPF 网格内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6785815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:59:42  来源:igfitidea点击:

How to clear WPF Grid contents?

wpf

提问by Muad'Dib

I want to clear/remove ALL the contents of a Grid including RowDefinitions, how can I do that?

我想清除/删除网格的所有内容,包括 RowDefinitions,我该怎么做?

回答by Muad'Dib

myGrid.Children.Clear()will remove all child controls nested in the grid. myGrid.RowDefinitions.Clear()will remove all row definitions. myGrid.ColumnDefinitions.Clear()will remove all column definitions.

myGrid.Children.Clear()将删除嵌套在网格中的所有子控件。 myGrid.RowDefinitions.Clear()将删除所有行定义。 myGrid.ColumnDefinitions.Clear()将删除所有列定义。

for the sake of completness, you can also add/remove single items through the add/remove methods of the appropriate collections. myGrid.Childrenfor controls, myGrid.RowDefinitionsfor row definitions, and myGrid.ColumnDefinitionsfor the columns.

为了完整起见,您还可以通过相应集合的添加/删除方法添加/删除单个项目。 myGrid.Children用于控件、myGrid.RowDefinitions行定义和myGrid.ColumnDefinitions列。

all of this information is available here on MSDN

所有这些信息都可以在 MSDN 上找到

回答by Vincent Ducroquet

try to loop into your container control(Example a Grid) and in this loop check the type of the control like this :

尝试循环到您的容器控件(例如网格)并在此循环中检查控件的类型,如下所示:

     foreach(DependencyObject c in YourContainer.Children)
        {
            //If you only want to modify TextBoxes
            if(c.GetType().ToString() == "System.Windows.Controls.TextBox")
            {
                //Erase Text property of all TextBoxes in my Grid Control
                ((TextBox)c).Text = "";


            }
        }