Louis Kottmann
Louis Kottmann

Reputation: 16628

WPF set CaretPosition at start of a RichTextBox

I have a RichTextBox in my WPF app, filled with colored text. I want to make it programmatically go to the start of the document, so I followed msdn's recommandation:

    TextPointer caretPos = RTB.CaretPosition;
    caretPos = caretPos.DocumentStart;
    RTB.CaretPosition = caretPos;    

And that does precisely nothing...

I also tried:

RTB.Selection.Select(RTB.Document.ContentStart, RTB.Document.ContentStart);

Which didn't work either.

I couldn't find other information on the subject aside from another question in StackOVerflow, that remains unanswered.

Anyone has a solution?

Upvotes: 0

Views: 2294

Answers (2)

Louis Kottmann
Louis Kottmann

Reputation: 16628

Simple solution, don't touch the Caret! RTB.ScrollToHome(); did the deed.

Upvotes: 1

brunnerh
brunnerh

Reputation: 184416

I thought it would be this:

rtb.CaretPosition = rtb.Document.ContentStart;

Seems to work for me.

Upvotes: 4

Related Questions