Boris
Boris

Reputation: 10234

Get Xamarin Forms control actual width in code behind

Here is my Xamarin Forms XAML code:

<Grid x:Name="ToggleIndicator" HorizontalOptions="FillandExpand" HeightRequest="4" />

I would like to get this control's width in code behind. ToggleIndicator.HeightRequest returns 4 which makes sense, however, how would I be able to get the actual width? ToggleIndicator.WidthRequest returns -1.

Upvotes: 0

Views: 346

Answers (1)

Nick Kovalsky
Nick Kovalsky

Reputation: 6442

The point is the width is changing from -1 to whatever, on rotation etc.. so:

   <Grid 
              SizeChanged="ToggleIndicator_OnSizeChanged"
              x:Name="ToggleIndicator" HorizontalOptions="FillandExpand" HeightRequest="4" />

codebehind:

private void ToggleIndicator_OnSizeChanged(object sender, EventArgs e)
{
    var grid = sender as Grid;
    if (grid != null)
    {
        //check my grid.Width
    }
}

Upvotes: 2

Related Questions