views:

293

answers:

1

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>
+2  A: 

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"&gt;
  <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"&gt;
  <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"&gt;
  <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.

Robert Rossney
I've found the offending code and posted it as an edit to my question. Although I have found where the problem is located I still can't figure out why it is happening.
Justin
Thank you very much!
Justin