0

Consider the below WPF -

enter image description here

The issue is the text that appears in the label is being cut off if it is longer than the width of the label.

XAML - label name is 'factTxt'

<Window x:Name="mainScreen" x:Class="FactsOnAll.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FactsOnAll"
        mc:Ignorable="d"
        Title="Random Facts" Height="450" Width="800" Icon="Properties/checklist.png">
    <Grid>
        <Button x:Name="getFactBtn" Content="Get Random Fact" HorizontalAlignment="Left" Margin="304,331,0,0" VerticalAlignment="Top" Width="177" Click="getFactBtn_Click" FontSize="20" Background="#FF8ECF87" Foreground="#FF444444"/>
        <Label x:Name="factTxt" Content="" Margin="10,98,10,0" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="20"/>
        <Label x:Name="titleTxt" Content="Random Facts" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="772" Height="46" FontSize="24"/>

    </Grid>
</Window>

Expect result

Allow the text to go to a new line. I thought increasing the height of the label would fix this but no luck.

Aleksandar Zoric
  • 1,343
  • 3
  • 18
  • 45
  • A `Label` is not meant to display Text with multiple lines. Have you tried to use a `Textblock`? (Dont forget to set the `Property` `TextWrapping="Wrap"`) – LittleBit Oct 19 '18 at 11:59
  • Possible duplicate of [How can I wrap text in a label using WPF?](https://stackoverflow.com/questions/5013067/how-can-i-wrap-text-in-a-label-using-wpf) – FakeCaleb Oct 19 '18 at 11:59
  • @LittleBit, just switched to a text block and current issue is fixed. Trying to figure out how to centre text in a text block? I switched over from Windows forms so first time using WPF. Horizontal and vertical alignment in properties have no effect. – Aleksandar Zoric Oct 19 '18 at 12:05
  • Are you looking for the `TextAlignment="Center"` Property? – LittleBit Oct 19 '18 at 12:08

1 Answers1

2

The Label has no wrapping but a TextBlock. You can just create custom content like, that will wrap automatically:

<Label Width="100">
  <TextBlock Text="Get Random Fact" TextWrapping="Wrap"/>
</Label>
juri
  • 38
  • 4