Enviroment
I am developing a Windows Store App I created a TextBox with wordwrapping like so
<ScrollViewer x:Name="ScrollView" Margin="0" Grid.Row="1" Background="Black" BorderThickness="2" BorderBrush="White" KeyDown="keyDown" VerticalScrollBarVisibility="Auto" HorizontalScrollMode="Disabled" ZoomMode="Disabled" IsHorizontalRailEnabled="False" IsHorizontalScrollChainingEnabled="False">
<Grid x:Name="Canvas" SizeChanged="setConsoleWidth">
<TextBox x:Name="textbox" Text="TextBox" Foreground="White" FontSize="24" FontFamily="Assets/inconsolata.ttf#inconsolata" AcceptsReturn="True" BorderThickness="0" Background="{x:Null}" SelectionChanged="handleCaret" Margin="0" IsTextPredictionEnabled="False" TextWrapping="Wrap"/>
<Rectangle x:Name="Caret" Stroke="Black" Fill="White" StrokeThickness="0" Margin="571,260,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="0" Height="0"/>
</Grid>
</ScrollViewer>
The idea is that you can vertically scroll but not horizontally scroll in “textbox”.
Problem
I want the textbox to wrap all characters outside of the boundary to a new line. Words do not have to be completly preserved. When a character is outside the boundary that character should appear on a new line. Moreover, the previous character should stay on the previous line. This should be done with all characters including whitespace.
Current behavior
The current behavior can be explained with the following image:

Figure 1: Example behavior
When the caret is located at the last character of our line (Figure 1.1) two things can happen when you enter a character, depending on the character type:
-
Character type:
non-space (including tabs)all charactersCurrent behavior
The word you are appending characters to is simply wrapped to the newline. See figure 1.2.Desired behavior
Only the character is wrapped to the newline, in case of figure 1.2, the character ‘d’
will only be on the second line. -
Character type: spaceCurrent behavior
The space and index is not wrapped and just continues on the current line (see figure 1.3). Inserting another space will not make that space wrap (see figure 1.4, the caret is out of screen). The next non-space character is wrapped to the newline, but the space character will just stay on the first line.Desired behavior
The space is wrapped to the newline, in case of figure 1.2, the character ‘d’
will only be on the second line.
How do achieve this desired behavior (The page is backed by c#)?
Edit:
I was able to change the character-space behavior into the non-space behavior by replacing all spaces with nonbreaking-spaces(“\u00A0”). Now I only need a solution for the first part.
Following your idea you could insert a zero-width space after each character. It is the unicode symbol 200B (http://en.wikipedia.org/wiki/Zero-width_space). I just tried with Windows Forms and there it works.