RichTextBox is not working properly

When I first run my program and type some text in richtextbox, if I press enter, which causes the carat to go to a new line and then press back instead of going to the previous line, it just moves the space even if I did not type the text on this new line. I can not understand what is happening. What am I doing wrong? And how can I fix this?

Edit:

It seems like richtextbox adds space in front of the first character when I start typing, and a space is added before each new line. I cannot put a carat in front of the space by clicking in front of it, but I can remove it using the back space, and then everything will return to its normal state.

Edit 2: This is the code that seems to be causing the problem, but I can't figure out why it does this:

 <RichTextBox.Resources> <Style TargetType="{x:Type Paragraph}"> <Setter Property="Margin" Value="5"/> </Style> </RichTextBox.Resources> 
+4
source share
1 answer

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.

+4
source

Source: https://habr.com/ru/post/1302634/


All Articles