c# - How can I wrap text in a label using WPF?

ID : 11680

viewed : 164

Tags : c#.netwpflabelword-wrapc#

Top 5 Answer for c# - How can I wrap text in a label using WPF?

vote vote


The Label control doesn't directly support text wrapping in WPF. You should use a TextBlock instead. (Of course, you can place the TextBlock inside of a Label control, if you wish.)

Sample code:

<TextBlock TextWrapping="WrapWithOverflow">     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec adipiscing     nulla quis libero egestas lobortis. Duis blandit imperdiet ornare. Nulla     ac arcu ut purus placerat congue. Integer pretium fermentum gravida. </TextBlock> 
vote vote


Often you cannot replace a Label with a TextBlock as you want to the use the Target property (which sets focus to the targeted control when using the keyboard e.g. ALT+C in the sample code below), as that's all a Label really offers over a TextBlock.

However, a Label uses a TextBlock to render text (if a string is placed in the Content property, which it typically is); therefore, you can add a style for TextBlock inside the Label like so:

<Label                   Content="_Content Text:"     Target="{Binding ElementName=MyTargetControl}">     <Label.Resources>         <Style TargetType="TextBlock">             <Setter Property="TextWrapping" Value="Wrap" />         </Style>     </Label.Resources>  </Label>  <CheckBox x:Name = "MyTargetControl" /> 

This way you get to keep the functionality of a Label whilst also being able to wrap the text.

vote vote


I used the following code.

    <Label>         <Label.Content>             <AccessText TextWrapping="Wrap" Text="xxxxx"/>         </Label.Content>     </Label> 
vote vote


You can put a TextBlock inside the label:

<Label>    <TextBlock Text="Long Text . . . ." TextWrapping="Wrap" />  </Label>  
vote vote


To wrap text in the label control, change the the template of label as follows:

<Style x:Key="ErrorBoxStyle" TargetType="{x:Type Label}">     <Setter Property="BorderBrush" Value="#FFF08A73"/>     <Setter Property="BorderThickness" Value="1"/>     <Setter Property="Foreground" Value="Red"/>     <Setter Property="Background" Value="#FFFFE3DF"/>     <Setter Property="FontWeight" Value="Bold"/>     <Setter Property="Padding" Value="5"/>     <Setter Property="HorizontalContentAlignment" Value="Left"/>     <Setter Property="VerticalContentAlignment" Value="Top"/>     <Setter Property="Template">         <Setter.Value>             <ControlTemplate TargetType="{x:Type Label}">                 <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" CornerRadius="5" HorizontalAlignment="Stretch">                                           <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Content}"/>                 </Border>                                  </ControlTemplate>         </Setter.Value>     </Setter> </Style> 

Top 3 video Explaining c# - How can I wrap text in a label using WPF?