WPF文本框和空格字符包装

时间:2022-05-14 10:53:03

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.

在测试WPF应用程序时,如果我开始输入单词并到达文本框的最右边,那么基于我输入的最后空格/换行符,单词和光标将换行到下一行。

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.

如果在第一行,我输入一个字符,然后按住空格键,光标就会在文本框中滚动,当光标到达第一行的末尾时,它不会被换行到第二行。一旦我输入的不是空格,这个字符就会在第二行开始。如果我使用左箭头键向后移动,光标将会从第2行消失,在第一行的某个时候将不可见,直到它移动到以前类型的所有空格。如果我将光标放在第一行的末尾,然后键入另一个非空格字符,那么这个字符通常会在第2行前面的字符前面加上几个空格。相对于文本框中的文本,内容将包括所有可见字符以及包含在文本框视图区域之外的所有空格字符。

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 个解决方案

#1


1  

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?

一些研究和替代空间“”在文本上的不间断空格“/u00a0”使光标停留在文本框的边界内。我只是把它作为概念的证明,这将导致递归调用,所以不要在生产代码中使用它。或者使用转换器,或者数据绑定属性设置程序?

    /// <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;
    }

#1


1  

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?

一些研究和替代空间“”在文本上的不间断空格“/u00a0”使光标停留在文本框的边界内。我只是把它作为概念的证明,这将导致递归调用,所以不要在生产代码中使用它。或者使用转换器,或者数据绑定属性设置程序?

    /// <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;
    }