0

I have an WPF usercontrol which has a grid with 4 columns and only 1 row.

The second column is a WPF label.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>

    // I only specify there the label, no other components, just to simplify
    <Label Grid.Column="1"
           VerticalAlignment="Center"
           Margin="5"
           Content="{Binding Path=Text}"
           Foreground="{Binding Path=ForegroundColor}">
        <Label.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="TextWrapping" Value="Wrap" />
            </Style>
        </Label.Resources>
    </Label>
</Grid>

For some reason the text of the label is being cut-off at the end of the line instead of putting it at next line. I have tried above solution to wrap text and continue in the next line but it is not working. Any ideas?

UPDATE: I changed the Label as below according to this:

    <Label Grid.Column="1"
           VerticalAlignment="Center"
           VerticalContentAlignment="Center"
           HorizontalContentAlignment="Stretch"
           HorizontalAlignment="Stretch"
           Height="Auto"
           Margin="5"
           Foreground="{Binding Path=ForegroundColor}">
        <TextBlock Text="{Binding Path=Text}" TextWrapping="Wrap"/>
    </Label>

and now it is working like a charm!

Willy
  • 9,848
  • 22
  • 141
  • 284
  • 1
    Does it have enough space to wrap? Can we have a screenshot? – Fildor Jul 18 '22 at 13:51
  • Hi Rodri, glad to know you've found the solution to resolve this issue! Please consider answering it and accepting it as an answer to change its status to Answered. It will also help others to solve a similar issue. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jiale Xue - MSFT Aug 02 '22 at 03:19

1 Answers1

1

You may use the AccessText and set the TextWrapping="Wrap":

    <Label Grid.Column="1"
           VerticalAlignment="Center"
           VerticalContentAlignment="Center"
           HorizontalContentAlignment="Stretch"
           HorizontalAlignment="Stretch"
           Height="Auto"
           Margin="5"
           Foreground="{Binding Path=ForegroundColor}">
        <Label.Content>
            <AccessText TextWrapping="Wrap" Text="{Binding Path=Text}"/>
        </Label.Content>
    </Label>
Behnam
  • 337
  • 1
  • 6