RichTextBox 无法正常工作
当我第一次运行程序并在 Richtextbox 中键入一些文本时,如果我按 Enter 键,这会导致克拉移动到新行,然后按退格键而不是转到上一行,它只会向后移动一个空格,即使我尚未在此新行上输入任何文本。我不明白发生了什么事。我做错了什么?我该如何解决它?
编辑:
当我开始输入时,看起来 richtextbox
在第一个字符前面添加了一个空格,并且在我创建的每个新行前面添加了一个空格。我无法通过单击克拉前面来将克拉放在空格前面,但我可以使用退格键将其删除,然后一切都会恢复正常。
编辑2:这似乎是导致问题的代码,但我不明白为什么会这样做:
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="5"/>
</Style>
</RichTextBox.Resources>
When I first run my program and type some text in the richtextbox, if I press enter, which causes the carat to move to a new line, and then press backspace instead of going to the previous line it just moves back a space even though I haven't typed any text on this new line. I can't figure out what is happening. What am I doing wrong? And how can I fix it?
Edit:
It looks like the richtextbox
is adding a space in front of the first character when I start typing and a space is added in front of every new line I make. I cannot place the carat in front of the space, by clicking in front of it, but I can delete it by using backspace and then everything goes back to normal.
Edit 2: This is the code that seems to be causing the problem, but I can't figure out why it is doing that:
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="5"/>
</Style>
</RichTextBox.Resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我只是将其放入 Kaxaml 中,那么
RichTextBox
的行为就不是这样的:这让我怀疑您的代码中还存在其他问题。它是什么?
编辑:
好吧,很清楚为什么您会在插入符号前面出现“空格”:您正在将一种样式应用于设置边距的段落。 完全不清楚的是为什么按 BACKSPACE 会使它消失。
如何解决这样的问题:向您的
RichTextBox
添加一个事件处理程序(我使用KeyUp
),并使用XamlWriter
转储其>Document
属性到Console.Out
。您会看到,首次填充时,Document
包含:按 BACKSPACE 后,它看起来像这样:
如果您将样式中的边距设置为 50,则发生的情况会更明显5. 您在段落前面看到的“空格”实际上是段落的左边距,由 RTB 资源字典中的样式设置。由于该段落没有本地
Margin
属性,因此它从样式继承边距。当您按 BACKSPACE 时,左边距将设置为 0。这为段落的 Margin 属性提供了本地值,因此它停止从样式继承。
如果按 Enter 并添加新段落,新段落将复制前一个段落的页边距。所以,从本质上讲,你的风格不再有效。
这似乎是 RTB 实现 EditingCommands.Backspace 的方式中的一个错误。该命令正式应该做什么:
我认为很明显这不是它实际做的。事实上,如果将边距设置为 50,很明显,当插入符号位于有边距的段落开头时,并且按 BACKSPACE,该段落的左边距会减少 20。我看不到任何记录在案的理由对于这种行为。
那么,你能对此做些什么呢?这在某种程度上取决于您首先设置该边距的原因。从您最初的描述来看,听起来您认为这是边距,而不是 BACKSPACE 使它消失的事实,这就是错误。嗯,这很容易解决;摆脱那种风格。
但如果你出于某种原因需要这个余量,我不知道该告诉你什么。
That's not how the
RichTextBox
behaves if I just put this into Kaxaml:This leads me to suspect that there's something else going on with your code. What is it?
Edit:
Well, okay, it's clear why you're getting the "space" in front of the caret: you're applying a style to paragraphs that sets a margin. What's not clear at all is why pressing BACKSPACE makes it go away.
How to troubleshoot a problem like this: Add an event handler to your
RichTextBox
(I usedKeyUp
), and useXamlWriter
to dump itsDocument
property toConsole.Out
. You'll see that when it's first populated, theDocument
contains:After you hit BACKSPACE, it looks like this:
It's a lot more apparent what's going on if you set the margin in your style to 50 instead of 5. The "space" that you're seeing in front of the paragraph is in fact the paragraph's left margin, set by the style in the RTB's resource dictionary. Since the paragraph has no local
Margin
property, it's inheriting the margin from the style.When you press BACKSPACE, the left margin gets set to 0. This gives the paragraph's Margin property a local value, so it stops inheriting from the style.
If you hit ENTER and add a new paragraph, the new paragraph copies the margins of the previous paragraph. So, essentially, your style stops working.
This seems like it's a bug in how the RTB implements
EditingCommands.Backspace
. What that command's supposed to do, officially:I think it's pretty clear that's not what it's actually doing. In fact, if you set the margin to 50, it becomes clear that when the caret's at the beginning of a paragraph with a margin, and you press BACKSPACE, it reduces that paragraph's left margin by 20. I can't see any documented justification for that behavior.
So, what can you do about this? It sort of depends on why you're setting that margin in the first place. From your original description, it sounds like you think it's the margin, and not the fact that BACKSPACE makes it go away, that's the bug. Well, that's easy enough to fix; get rid of that style.
But if you need that margin for some reason, I don't know what to tell you.