In WPF, long word doesn't wrap in the first line if there's some text before it

85 Views Asked by At

I'm having trouble with wrapping some text in a TextBox in WPF.
Let's say I have following text "Here is some VeryLongTextThatHasToBeWrapped and so on".

<TextBox Text="Here is some VeryLongTextThatHasToBeWrapped and so on" TextWrapping="Wrap" MaxWidth="70" Height="150"/>

I'd expect that text would wrap in the first line as it doesn't fit in the TextBox. Like here:
Expected wrapping

However wrapping happens on the next line, so I have unused space and the whole text does't fit in the TextBlock. Like here:
Actual wrapping

I tried to play with Width and Height , tried to set TextWrapping to WrapWithOverflow .
Also, I played aroud with Run elements with InlineUIContainer with no luck.

Still can't get the desired result.
Is there any way to "force" wrapping to break a word as soon a possible instead of giving it maximum possible space and then breaking it?

1

There are 1 best solutions below

0
Rekshino On

You can consider string as array of chars and present it in an ItemsControl, where you set ItemsPanel to WrapPanel, of course it's then not editable.

<ItemsControl ItemsSource="Here is some VeryLongTextThatHasToBeWrapped and so on" MaxWidth="70">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>