开发者

Label word wrap in Textblock not working

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">开发者_Python百科;
   </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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜