WPF:富文本框中Tab键的问题

3

我很困惑这里发生了什么。我有一个rtb,而且我将allowTab设置为true,所以用户可以缩进。

问题是,在文本前面似乎没有制表符。例如,如果我的文本如下:

this is some text
    and some more text
    and one more line

我猜,如果我在代码中查看文本,它看起来像这样:

this is some text \r\n\tand some more text\r\n\tand one more line

但实际上它只是看起来像这样:
this is some text \r\nand some more text\r\nand one more line (notice there are 
no tabs)

选项卡对我非常重要,因为我需要追踪行的“层次结构”,我需要知道行前面有多少空格或制表符(\t)。当我在代码中查看字符串时,是否有办法将制表符空间显示为\t?
我应该捕捉按键事件,用空格替换制表符键事件吗?这就是我在winforms项目中所做的,但我想WPF会有一些很好的处理方法:)
谢谢!
编辑 当我按tab键时,它是否会更改段落文本缩进?也就是说,文本本身没有明显的区别吗?
1个回答

0

我不确定你是如何获取文本的,但它应该可以工作,测试:

string text = "this is some text \r\n\tand some more text\r\n\tand one more line";

richTextBox1.AppendText(text);

text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
//text will be the same: "this is some text \r\n\tand some more text\r\n\tand one more line\r\n"

1
好的,那么您需要检查每个块的TextIndent,例如:(richTextBox1.Document.Blocks.FirstBlock)).ContentStart.Paragraph.TextIndent - Jalal Said

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接