9
votes

My Observations:

In testing a WPF application with a Wrap enabled TextBox to allow for multi-lines of text, if I just start typing words and I reach the far right side of the TextBox, words and cursor wrap to the next line based on the last space/break of characters in my typing.

If on the 1st line I type a single character and then hold down the spacebar, the cursor scrolls out of view in the TextBox and does not wrap to the 2nd line when the cursor reaches the end of the 1st line. Once I type something other than a space, that character wraps and starts at the beginning of the 2nd line. If I use the left arrow key to move backwards, the cursor will disappear from the 2nd line and will not be visible for sometime on the 1st line until it moves thru all of the spaces that were previously type. If I place the cursor at the end of the 1st line and type another non-space character, that character generally wraps to the 2nd line with several spaces in front of the previous character on the 2nd line. Relative to the text in the TextBox, the contents will include all of the visible characters plus all the space characters contained outside of the view area of the TextBox.

My Question:

Is there a property setting on the TextBox that I am missing to force the space characters to wrap the 2nd line once they reach the end of the 1st line rather than scrolling off of the screen?

Thanks.

Mark

1
do you use Wrap or WrapWithOverflow?thumbmunkeys
Wrap but I have tried both w/ the same result.Mark Parr
not sure what the issue might be, but it seems that the spaces are interpreted as a an unwrappable word, see also this: stackoverflow.com/questions/13161966/…thumbmunkeys
Yeah -- that's what I was seeing/thinking too. I was hoping there was just some property I was overlooking. Along the lines of the link comments (and I had found that link earlier while trying to find info on the problem), I was thinking I might have something in my app as the root of the issue so I created a brand new, simple program w/ just a textbox to make sure I was in a base/default mode as much as possible and I still saw the same issue.Mark Parr
did you finally solve it or not?WPFKK

1 Answers

1
votes

Did some research and replacing space " " with nonbreaking space "/u00a0" on text changed keeps the cursor inside the bounds of the textbox.

I simply wired this up as a proof of concept, this will result in recursive calls so don't use this in production code. Maybe use a converter instead, or maybe in your data-bound property setter?

    /// <summary>
    ///     Handle textbox on text changed event
    /// </summary>
    private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        // Replace space with non-breaking space
        TestTextBox.Text = TestTextBox.Text.Replace(" ", "\u00a0");
        // Put the cursor at the end of the textbox (updating text sets the cursor at the start)
        TestTextBox.CaretIndex = TestTextBox.Text.Length;
    }