Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms BoxView Height match Width

I am trying to get the box view of a content page to 'FillAndExpand' horizontally while making the height equal to it's width. So far for the xaml I've got:

<ContentPage Title="About" >
<StackLayout>
    <BoxView x:Name ="imageBoxView" Color="AliceBlue" HorizontalOptions="FillAndExpand" />
</StackLayout>
</ContentPage>

But I don't know what value to keep for the Height Request.

like image 875
Samuel Mungy Avatar asked Mar 18 '18 03:03

Samuel Mungy


1 Answers

You can set the binding context to itself, and then bind the HeightRequest to the view's Width.

Note: This will not work in the XAML preview mode in Visual Studio, but will work at runtime on the device.

    <StackLayout>
       <BoxView x:Name ="imageBoxView" Color="AliceBlue"
          HorizontalOptions="FillAndExpand" 
          BindingContext="{x:Reference imageBoxView}" 
          HeightRequest="{Binding Width}" />
    </StackLayout>
like image 69
SushiHangover Avatar answered Sep 28 '22 09:09

SushiHangover