Vorotnyak_Nazar
Vorotnyak_Nazar

Reputation: 914

How to get Height of StackLayout after adding dynamically a view?

I have a StackLayout (vertical options is Start). I need to add a view to it and after that to get height of StackLayout. How can I do that? Now after adding a view into StackLayout in code behind I get Height property equals 0.

Upvotes: 4

Views: 7588

Answers (2)

Vorotnyak_Nazar
Vorotnyak_Nazar

Reputation: 914

I added a view into stacklayout like this

StackLayout currentColumn = columns.ElementAt(currentColumnIndex);

currentColumn.Children.Add(orderCard);

And wanted to get currentColumn.Height, but it was 0.

Instead I used Measureand that gave a correct Height

SizeRequest columnSizeRequest = currentColumn.Measure(OrdersContainer.Width / 5, OrdersContainer.Height);

Upvotes: 3

Yuri S
Yuri S

Reputation: 5370

In what part of code you are trying to get the size? It will not work if you check it in constructor or right after you add the view. What you can do in constructor is:

        layout.SizeChanged += Layout_SizeChanged;

and then you will get correct size

private void Layout_SizeChanged(object sender, EventArgs e)
{
    var h = layout.Height;
}

Upvotes: 9

Related Questions