This is not how the RichTextBox behaves if I just put this in 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 makes me suspect something is happening with your code. What is it?
Edit:
Well, itβs clear why you get βspaceβ in front of the carriage: you apply a paragraph style that sets margins. It is unclear why pressing the BACKSPACE button forces him to leave.
To fix the problem as follows: add an event handler to your RichTextBox (I used KeyUp ) and use XamlWriter to reset its Document property to Console.Out . You will see that when it is 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 press 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 is much more obvious what happens if you set the field in your style to 50 instead of 5. The "space" that you see before the paragraph is actually the left edge of the paragraph, the style set in the RTB resource dictionary. Since the paragraph does not have a local Margin property, it inherits the margin from the style.
When you press BACKSPACE, the left margin gets the value 0. This gives the Margin margin property a local value, so it no longer inherits from the style.
If you press ENTER and add a new paragraph, the new paragraph copies the fields of the previous paragraph. Therefore, in essence, your style ceases to work.
This seems to be a bug in how RTB implements EditingCommands.Backspace . What this team should have done officially:
When invoked with an empty choice, this command removes the delimiter of characters or paragraphs immediately before the carriage. When a non-empty selection is invoked, this command deletes the selection.
This command saves any formatting from remote fetching for content that is immediately pasted in the same place after calling this command.
I think it is pretty clear that this is not what he actually does. In fact, if you set the field value to 50, it becomes clear that when the carriage at the beginning of the paragraph is marked and you press BACKSPACE, it reduces that paragraph, leaving the field at 20. I do not see any documented justification for this behavior.
So what can you do about this? It depends on why you set this margin in the first place. From your original description, it sounds like you think it is an edge, not the fact that BACKSPACE makes it go away, which is a mistake. Well, it's easy enough to fix; get rid of this style.
But if you need this margin for some reason, I donβt know what to tell you.