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.