Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label word wrap in Textblock not working

Tags:

word-wrap

wpf

I have a WPF page with quite a few objects. At the bottom of all these items, I have a label that needs to have textwrapping in the content. That answer is simple, with the use of a Textblock this should be a cinch. However, even though I use those items, I still can't get th e text to wrap. So I'm assuming that there must be other settings in the other objects that I need to check/modify. In pseudo code, my XAML looks like

<Page>
  <Stackpanel vertical>
   <Border>
    <Stackpanel vertical>
     <label></label>
    <Stackpanel horizontal>
     <label></label>
    </stackpanel>
   <label>
    <textblock TextWrapping="Wrap">
   </label>
  </border>
 </stackpanel>
</page>

What am I missing here? Should I be checking other elements? I have already made sure that none of the nesting elements have any height specified - they are all set to auto.

like image 200
Unknown Coder Avatar asked Feb 24 '23 07:02

Unknown Coder


1 Answers

I'm going to go ahead and post this as an answer in case someone else gets stuck on this.

When you set the Width and Height properties of the TextBlock control, it will first horizontally increase in size to accommodate as much text as it can before wrapping the text (if the TextWrapping property is set to Wrap). Once it decides it needs to wrap the text, if the Height is set to Auto, the text box will resize vertically to accommodate the text (until it hits the bottom of whatever UI container you put it in). If you do not want the text box to resize past a certain point, you will need to set values for the MaxWidth and MaxHeight properties. This will force the TextBlock to wrap at a certain width.

like image 53
Chad La Guardia Avatar answered Feb 25 '23 20:02

Chad La Guardia