This is a migrated thread and some comments may be shown as answers.

Get/Set scrollviewer-position, cursor, selection and zoom

5 Answers 527 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Philipp
Top achievements
Rank 1
Philipp asked on 14 Jan 2014, 03:52 PM
Hello,

is it possible to set and get the following infomation (via viewmodel)?

- position of the scrollviewer
- absolute position of the cursor (e.g. character 1234 and not x and y from the carret)
- selection
- zoom level

King regards,
Philipp

5 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 17 Jan 2014, 11:49 AM
Hello Philipp,

Thank you for the question!

When it comes to the scroll offset, selection and zoom level, they are part of RadRichTextBox's UI so accessing them from your view model will not be possible.

As to your other point, could you please elaborate a bit on what exactly you mean? As far as I understand, you want to be able to determine the position in the document similarly to how this is done in a string (i.e. in the "test" string the first position is 't'), is that correct? Generally, RadDocument's hierarchy contains different elements not all of which have visual representation in the document (for example bookmarks and other annotations).Furthermore, document elements are not associated with length, nor are document positions with a particular letter in the word since there is other content that might be present on a position - annotation, image, formatting symbol, etc. For this reason mapping a document position to an index is not possible.

Do not hesitate to get back to us if you have any further questions.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Philipp
Top achievements
Rank 1
answered on 20 Jan 2014, 09:08 AM
Hello Petya,

thank you for your answer.
Can you give me an example to get/set the scroll offset and zoom level via code behind?

For the other point: I want to get the position including the rtf tags (if I use a RtfDataProvider) and not only the text I see in the editor. But I guess that#s not possible, isn't it?

I've another question: is it possible to set bookmarks via code or to move the cursors to one specific bookmark via code?

King regards,
Philipp
0
Accepted
Petya
Telerik team
answered on 21 Jan 2014, 11:58 AM
Hello Philipp,

Thanks for getting back to us! Straight to your questions:

1)Obtaining the vertical offset and respectively scrolling to particular offset can be achieved in the following way:
double offset = this.radRichTextBox.VerticalOffset;
this.radRichTextBox.ScrollToVerticalOffset(offset);

2) Obtaining/setting the zoom level can be achieved using the ScaleFactor property of RadRichTextBox:
Size size = this.radRichTextBox.ScaleFactor;
this.radRichTextBox.ScaleFactor = new Size(0.8, 0.8);
In this case Size(1,1) is equivalent to 100% zoom level.

3) Adding a bookmark from code is possible with the InsertBookmark() method:
this.radRichTextBox.InsertBookmark("bookmarkName");
You can then move the caret to the bookmark using its name by simply calling the GoToBookmark method of RadDocument or like this:
BookmarkRangeStart bookmark = this.radRichTextBox.Document.GetBookmarkByName("bookmarkName");
this.radRichTextBox.Document.CaretPosition.MoveToInline(bookmark);

When it comes to your comment about RTF, I am afraid I am not sure what you mean. Could you give some kind of example so that we can better understand your needs?

I hope this helps! We're awaiting your reply.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Philipp
Top achievements
Rank 1
answered on 22 Jan 2014, 10:11 AM
Hello Petya,

thank you for the detailed information.

@ RTF problem:
I have an editor with the text "this is a test".
I have a RtfDataProvider (or XAML, or Docx, doens't matter) and I click into the editor - behind the s from this.
I want to find out where the cursor is. But i want to get the positio from the bound string which is something like "<some rtf header content>this is a test<some rtf footer content>".
Background is: I want to do some operations in my viewmodel and I need the correct position to insert e.g. some text.

King regards,
Philipp
0
Accepted
Petya
Telerik team
answered on 24 Jan 2014, 02:15 PM
Hello Philipp,

Thank you for providing us with additional details!

The scenario you wish to implement is not possible out of the box. However, what you could try is creating a custom annotation. For example, you could inherit the AnnotationMarkerBase class and create a simple marker-like annotation which does not require pairing like most other annotations in the context of RadRichTextBox. 

This setup would allow you to insert an instance of the marker at the caret position. Upon exporting the document, the marker will be preserved which would allow you to make modifications at its particular position. What you should be aware of in this regard is that custom annotations are only preserved when exporting to XAML format.

Additionally, I am not sure how you could go about wiring the logic inserting the annotation. Generally, if the class is called CaretPositionMarker, here is how you can add a marker at the caret position:
this.radRichTextBox.InsertInline(new CaretPositionMarker());
However, I'd not recommend placing logic executing this action in the PositionChanged event as the event is raised extensively when using RadRichTextBox and this might cause performance issues. In case you are using a data provider in your application you could set its UpdateSourceTrigger property to Explicit or LostFocus and only add the marker when updating the string (with the UpdateString() method of the provider). Please find attached a simple sample project demonstrating how this can be achieved.

I hope this helps! Let me know if I can provide further assistance.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
RichTextBox
Asked by
Philipp
Top achievements
Rank 1
Answers by
Petya
Telerik team
Philipp
Top achievements
Rank 1
Share this question
or