user342552
user342552

Reputation:

How to clear WPF Grid contents?

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

Upvotes: 15

Views: 26337

Answers (2)

Vincent Ducroquet
Vincent Ducroquet

Reputation: 1

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 = "";


            }
        }

Upvotes: -2

Muad'Dib
Muad'Dib

Reputation: 29216

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.

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

all of this information is available here on MSDN

Upvotes: 29

Related Questions