M11CH
M11CH

Reputation: 46

How to remove dynamically added grid element in UWP

I have a page in my UWP application where the user can choose a product from the combo box.

enter image description here

There are 4 types of selectable products all together but 2 of them fire an event. If the user chooses "Pizza" or "Sandwich", a new Grid element is added to the display which contains toppings via the following code:

        Grid toppingGrid = new Grid();
        toppingGrid.Name = "toppingGrid";
        toppingGrid.Width = 1000;
        toppingGrid.Margin = new Thickness(0, 500, 0, 0);
        toppingGrid.HorizontalAlignment = HorizontalAlignment.Center;

Then there is some other code in the middle which adds the toppings into this topping grid, and finally I add the topping grid into the main grid on my page:

this.detailsGrid.Children.Add(toppingGrid);

Now when the user decides to change from Pizza to a different product, I have to remove this newly created topping grid element from display. I have an event listener on the product type combo box which fires each time the product is change, but I'm not sure how to remove the topping grid. I tried the following but it doesn't seem to work:

if (this.detailsGrid.Children.Count > 6)
            this.detailsGrid.Children.RemoveAt(7);

Upvotes: 0

Views: 146

Answers (1)

Vincent
Vincent

Reputation: 3304

You can use Visibility property.

If your top grid's child is a TextBlock, just control it's Visibility.

Hope it can help you.

enter image description here

Upvotes: 1

Related Questions