4

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

Mirwais
  • 131
  • 1
  • 4
  • 18

1 Answers1

10

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.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
  • This resulted in only seeing " 1. C ".. So I changed MaxWith to 1000 and It almost worked. I see the first line and the second line is half visible – Mirwais Jan 21 '16 at 10:54
  • sorry, I thought you might get idea by that. you have to set the height also according to your text. If you give me your exact text than I would be able to tell you what should be the MaxWidth and height of textblock. it's a combination of MaxWidth and Height and create a perfect rectangle for your text. – Kylo Ren Jan 21 '16 at 10:59
  • 1
    So stupid of me. You are right, I just saw the height="50".. Fixed it! Thank you! – Mirwais Jan 21 '16 at 11:03