2
votes

In the code-behind of a XAML page I'm checking whether a text field from my model is null, if not I update a labels text property with the field, if it is null I want to completely remove it from the XAML.

At first I tried to set the IsVisible to false and the HeightRequest to 0, but I could still see the space in the stackLayout where the label would have been, so I tried removing inner stack that contains the label from the main stack

TopStack.Children.Remove(addressStack);

But again I can still see a space where the removed stack would have been.

I there a way to remove the inner stack completely, including the space it would have occupied?

1
You probably need to call the InvalidateMeasure after removal - Gerald Versluis
Did you try to call ParentStack.ForceLayout() after set ParentStack.Children[X].IsVisible = false;? - Diego Rafael Souza

1 Answers

5
votes

You can use the theStack.Children.Remove(textField); to remove the element from the stack.

If you know the position of the element in the stack, you can use the remove at

  theStack.Children.RemoveAt(positionInStack);

If you want to remove the inner stack, which is a child to the outer stack and a parent to the text field, use the

outerStack.Children.Remove(textField.parent);

I hope this information will help you!