RichTextBox not working properly

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?


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:

     <Style TargetType="{x:Type Paragraph}">
            <Setter Property="Margin" Value="5"/>


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?


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="">
  <Paragraph />

After you hit BACKSPACE, it looks like this:

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="">
  <Paragraph Margin="0,5,5,5" />

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.

Need Your Help

Putting an Anonymous collection into a Class

c# linq generics

I have a link query which returns different lists of data to fill different Grids on webpages,

Emacs name of current local keymap?

emacs key key-bindings major-mode

I am writing an elisp function that permanently binds a given key to a given command in the current major mode's keymap. For example,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.