RichTextBox 无法正常工作

发布于 2024-08-22 20:23:23 字数 529 浏览 3 评论 0原文

当我第一次运行程序并在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-08-29 20:23:23

如果我只是将其放入 Kaxaml 中,那么 RichTextBox 的行为就不是这样的:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
    <RichTextBox/>
  </Grid>
</Page>

这让我怀疑您的代码中还存在其他问题。它是什么?

编辑:

好吧,很清楚为什么您会在插入符号前面出现“空格”:您正在将一种样式应用于设置边距的段落。 完全不清楚的是为什么按 BACKSPACE 会使它消失。

如何解决这样的问题:向您的 RichTextBox 添加一个事件处理程序(我使用 KeyUp),并使用 XamlWriter 转储其 >Document 属性到 Console.Out。您会看到,首次填充时,Document 包含:

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph />
</FlowDocument>

按 BACKSPACE 后,它看起来像这样:

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph Margin="0,5,5,5" />
</FlowDocument>

如果您将样式中的边距设置为 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:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
    <RichTextBox/>
  </Grid>
</Page>

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 used KeyUp), and use XamlWriter to dump its Document property to Console.Out. You'll see that when it's first populated, the Document contains:

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph />
</FlowDocument>

After you hit BACKSPACE, it looks like this:

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph Margin="0,5,5,5" />
</FlowDocument>

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:

When invoked on an empty selection, this command deletes the character or paragraph separator just before the caret. When invoked on a non-empty selection, this command deletes the selection.

This command retains any formatting from the deleted selection for content immediately inserted at the same location after this command is invoked.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文