Zure Zure - 1 month ago 7
C# Question

Set Label HeightRequest automatically depending on how much space the text needs

I have a

Label
inside a
StackLayout
inside a
Frame
:

<Frame Grid.Row="0" Margin="0, 0, -10, 0" OutlineColor="Transparent" HasShadow="False" Grid.Column="1" BackgroundColor="{StaticResource rightBubbleColor}">
<StackLayout>
<Label
Style="{StaticResource rightBubbleStyle}"
TextColor="{StaticResource rightBubbleFontColor}"
Text="{Binding message}" />
</StackLayout>
</Frame>


This gives me this result:

Label without HeightRequest

The problem here is that the last message actually has more text which would need 6 rows to display the whole text.

If I set a value for
HeightRequest
for the
Label
, it changes the height of the
Frame
as well. For example if I set a value of 150, I get this result:

Label with HeightRequest

I need to dynamically set a value for
HeightRequest
depending on how much height the
Text
needs.

Does anyone know how I can achieve that (preferably in xaml) or is this even a wrong approach to my problem?

Edit 1:



Removing the style from the
Label
doesn't solve the problem. It only occurs on UWP. Note that I don't have a Windows Phone to test it, the problem occurs on my local machine (Windows 10 Desktop).

Edit 2:



According to the Xamarin documentation, the height should be autosized dependon on the content:


When the app developer sets the ListView.HasUnevenRows property to true, the behavior of the list view still depends on the ListView.RowHeight property. First, if the developer either does not set the ListView.RowHeight property or sets it to -1, list view items are autosized to fit their contents. This is the desired behavior and the intended use case for a ListView.HasUnevenRows value of true, as noted above.

Answer

I found a solution. This was the problem:

<RowDefinition Height="*" />

I set the value to Auto and now it's working. Strange that it has a different behaviour on iOS and Android tho.