Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically next line in a label if a string is too long in XAML

I have a label that is binded to something. I want the label to expand in width and height if the string is too long, so that it fits on screen.

I had this:

<StackPanel Orientation="Horizontal" Margin="0,0,0,200" Height="50" Width="900">
     <Label HorizontalContentAlignment="Left" VerticalAlignment="Center" Padding="5" FontSize="24" Content="Instruction: " Width="290" />
     <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="25" Text="{Binding InstructionLabel}" Width="auto" Height="auto"  />
</StackPanel>

Notice that I tried using a TextBlock instead of a Label. This didn't work tough, so I tried:

<Label HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="25"  Width="auto" Height="auto">
      <AccessText TextWrapping="WrapWithOverflow" Text="{Binding InstructionLabel}"/>
</Label>

But this doesn't work either.

The View is now like this:

enter image description here

like image 591
Mirwais Avatar asked Dec 25 '22 10:12

Mirwais


1 Answers

just give the MaxWidth of your TextBlock

   <StackPanel Orientation="Horizontal" Margin="0,0,0,200" Height="50" Width="900">
       <Label HorizontalContentAlignment="Left" VerticalAlignment="Center" Padding="5" FontSize="24" Content="Instruction: " Width="290" />
      <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" MaxWidth="100" VerticalAlignment="Top" FontSize="25" Text="{Binding InstructionLabel}" Width="auto" Height="auto"  />
</StackPanel>

and then your textwrapping will work.

like image 56
Kylo Ren Avatar answered Dec 27 '22 21:12

Kylo Ren