Antonio Teh Sumtin
Antonio Teh Sumtin

Reputation: 498

WPF How to remove a grid?

In my WPF application I have created and added a new Grid, I already know how to remove all Children from it:

    private void ClrScr()
    {
        for (int i = GridName.Children.Count - 1; i >= 0; i--)
        {
            GridName.Children.RemoveAt(i);
        }
    }

But I have no idea how to kill the grid itself, my tries:

        GridName.Exit/Disable/Something; /// <--- no Idea what am I doing...
        this.Controls["GridName"].DIEEEE;

Sadly I have failed... I am very new to WPF, I've mostly played with WinForms... Help?

Upvotes: 1

Views: 3734

Answers (2)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

You can remove the grid using it's parent.

e.g: If you have three grids named grid1, grid2 and grid3 in a dock panel control named main, you can remove these grids like this:

main.Children.Remove(this.grid1);
main.Children.Remove(this.grid2);
main.Children.Remove(this.grid3);

Upvotes: 1

Dilshod
Dilshod

Reputation: 3311

All the controls in WPF has parent except form. If the Grid you are going to delete is the first then you can do this:

var parent = myGrid.Parent;
Window window = parent as Window;
if(window!=null)
    window.Content = null;

Sometimes Grid can be child of a control which doesn't have Content property. If it doesn't have Content then it must have Children or Items.

Upvotes: 0

Related Questions