Label word wrap in Textblock not working

喜欢而已 提交于 2019-12-23 15:33:31

问题


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.


回答1:


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.




回答2:


With my XamlPadX, this wraps:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Width="300">
  <StackPanel Orientation="Vertical">
    <StackPanel Orientation="Vertical">
     <Label>L1</Label>
     <StackPanel Orientation="Horizontal">
       <Label>L2</Label>
     </StackPanel>
     <Label>
       <TextBlock TextWrapping="Wrap">
         This text wraps.
         This text wraps.
         This text wraps.
         This text wraps.
         This text wraps.
         This text wraps.
         This text wraps.
       </TextBlock>
     </Label>
   </StackPanel>
 </StackPanel>
</Page>

So the problem must be somewhere else.



来源:https://stackoverflow.com/questions/7043952/label-word-wrap-in-textblock-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!