简体   繁体   中英

Xamarin Forms - removing a child element from a StackLayout in C#

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM