Dear support,
I have a very simple question I am struggeling with:
How can I scroll to the current caret location or any selection within a XamRichTextEditor?
I highly appreciate any quick help!
Thank you in advance,
Marc
Hello Marc,
In order to set the scroll position of the XamRichTextEditor, you can set the XamRichTextEditor.ActiveDocumentView.VerticalViewOffset property.
If you are looking to set this to a particular selection, you can get the selection’s offset by using the various properties on the Selection element returned by the XamRichTextEditor.Selection property. For example, you can use the DocumentSpan.Offset or use the Cells or Paragraphs collection to get the offset as well.
It is worth noting that you can also move to the very start or very end of the document by using the RichTextEditorCommand class as well. This can be done with the following code, where “rte1” is the XamRichTextEditor:
RichTextEditorCommand cmd = new RichTextEditorCommand(Infragistics.Controls.Editors.RichTextEditorCommandType.MoveToEndOfDocument); cmd.Execute(rte1);
I hope this helps. Please let me know if you have any other questions or concerns on this matter.
Dear Andrew,
In reply to my own previous post: I found a solution!
If one needs to scoll to a specific location, one needs to use the Y-pixel location of the caret. This seems necessary, because the XamRichTextEditor.VerticalViewOffset seems to be a vertical pixel offset of the overall XamRichTextEditor, while for instance FindReplaceManager.Current.Span.Offset seems to be a character offset.
Long story short, what worked for me:
My concrete use case is to simply highlight and scroll to the first occurrence of a string within a long text.
FindReplaceManager.MoveToNearestMatchFollowingOffset(0);FindReplaceManager.SelectCurrentMatch();Caret.SetDocumentOffset(FindReplaceManager.Current.Span.Start.Offset);Focus();ActiveDocumentView.VerticalViewOffset = Caret.PixelLocation.Y;
Hope, this helps other people.
Thank you, Andrew.
Bests,
Thank you for your updates on this matter. I am glad you were able to find a solution that worked for you. My apologies for the Selection suggestion that did not work for you.
Please let me know if you have any other questions or concerns on this matter.