Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a multiline label in StackLayout/Grid for UWP?

I'm using Xamarin.Forms and I haven't found out how to have a multiline label for UWP. In my real project I have the label in a Grid on a ViewCell in a ListView. For the below example I've put it into a StackLayout:

<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    <Label x:Name="titleLabel" Text="some long text" LineBreakMode="WordWrap" />
    <Label x:Name="valueLabel" Text="perhaps a much, much, much, much, much, much, much, much, much longer text" LineBreakMode="WordWrap" />
</StackLayout>

The code is working on iOS and Android, but not on UWP. Here the text gets cut off. If only one label and no StackLayout is used it appears correctly on UWP with the multiline text.

I also tried to use a custom renderer with a multiline TextBlock as label, but the result was the same - the text is cut off.

Is this a bug or am I missing something?

Edit:

Now I made a UWP project

<StackPanel Orientation="Horizontal">
    <TextBlock Text="oh this is a label" TextWrapping="Wrap" />
    <TextBlock Text="and this should be a very, very, very, very, very, very, very, very, very, very long text" TextWrapping="Wrap" />
</StackPanel>

and here the text is also cut off. It seems it isn't possible in UWP. Are there any other solutions/workarounds?

like image 962
testing Avatar asked Oct 14 '16 12:10

testing


1 Answers

If you set a WidthRequest for each label, the multiline label is correctly displayed (by growing in height).

An alternative solution is e.g. putting the label into a Grid and use a fixed column width.

The reason for that behavior could be that StackLayout has a default width of Auto (if nothing explicitly set) and the label doesn't know how big it could be. If you set a width, the label can grow up to that size.

This post brought me on the idea.

like image 141
testing Avatar answered Nov 05 '22 09:11

testing