Forum Xamarin.Forms

How to measure a ContentView based on its content ?

Vimal.4745Vimal.4745 USMember ✭✭

Hi All,

I am having a ContentView where i add DataTemplate as its content. Is there any way to measure the size of the content view based on its content. Please note that its content can be any view. I can measure the size of the content if the height and width are explicitly set when defining the DataTemplate like in the below case.

<DataTemplate> <Entry x:Name="numeric" Text="{Binding FirstName}" FontSize="12" TextColor="Black" WidthRequest=150 HeightRequest=100 Keyboard="Numeric" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" /> </DataTemplate>

But when the height and width are not specified , the measure call returns -1 as width and height which is the behavior of the framework, since the measure call fetches the size only from the WidthRequest and HeightRequest properties. Is there any way to overcome this and get the actual size of the view based on the content.

Awaiting your suggestions. Thanks in advance !

Vimal Prabhu

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I think the more important question is 'why'. Why do you feel you need to measure it? Content shouldn't depend on knowing its size. It could be numerous sizes depending on the platform, device and orientation. You're UI should be dynamic and adjust as needed; not requiring knowledge of its size.

  • Vimal.4745Vimal.4745 USMember ✭✭

    @ClintStLaurent said:
    I think the more important question is 'why'. Why do you feel you need to measure it? Content shouldn't depend on knowing its size. It could be numerous sizes depending on the platform, device and orientation. You're UI should be dynamic and adjust as needed; not requiring knowledge of its size.

    Hi Clint,

    I load a data template inside a column in my grid.
    Now i want to set the column size based on the content of the column.

    Now that i have answered your question of 'why' can you please revert with the answer for my question ?

    Thanks in advance !

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Now i want to set the column size based on the content of the column.

    That should be happening automatically. That's the default behavior for columns. You're already requesting size for the item in the datatemplate

    WidthRequest=150

    Are you saying that's not happening? Or have you not tried it yet? Can you provide a screen shot of what it is doing; because something seems wrong about its behavior.

    You are aware that you can't really set the size, right? All you can do is request. But everything else has to be rendered too. Maybe there is a conflict with getting everything on screen so your request can't be fully honored and the space has to be reduced in order to get everything to fit.

  • SagarPanwalaSagarPanwala USMember ✭✭✭

    @Vimal.4745 Did you find the solution ? I'm struggling with same.
    @ClintStLaurent I want to set CarouselView height based on it children who height is maximum.

Sign In or Register to comment.