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,
Hello Andrew,
Thank you very much for your quick answer!
Indeed, using XamRichTextEditor.VerticalViewOffset made me able to actually scroll within the RichTextEditor, but unfortunately it doesn't work as expected or desired.
If I set
MyTextEditor.VerticalViewOffset = FindReplaceManager.Current.Span.Offset
and the target is before the current view, I see the text editor jumping to a location that is closer to the selection but still far away. If the target is after the current view, the text editor scrolls to the very end but shows weired behavior because the content is first cleared (presenting an empty text editor) and when I scroll manually, it jumps to the very last line (which is not the actual target that was supposed to be scrolled to).
My guess is that the two offset-values do not match: the XamRichTextEditor.VerticalViewOffset and the FindReplaceManager.Current.Span.Offset-values are defined differently.
I highly appreciate further help here.