I have a few textboxs and I want to achieve this funcion: when tap into each textbox, that specific textbox will move to the top and when user type in some data it can auto scroll and keep the cursor|caret always on top of the screen. I know that I can useScrollViewer.ChangeView(null,offset,null);
to set the view, however, how can I get the cursor's position (y or vertical offset) though. This is WP 8.1 app.
Asked
Active
Viewed 194 times
-1

litaoshen
- 1,762
- 1
- 20
- 36
-
1What cursor are you expecting there to be on the phone? – Rowland Shaw Dec 16 '15 at 21:11
-
@RowlandShaw I used IBeam, which is something like "|", but I don't know how to get it's position. – litaoshen Dec 16 '15 at 21:14
-
Do you mean the **caret** then, not the *cursor*? – Rowland Shaw Dec 16 '15 at 21:34
-
@RowlandShaw Yes...I didn't know that it has the same name as ^ – litaoshen Dec 17 '15 at 00:13
2 Answers
1
According to the documentation for the WPF TextBox
class here, there is a property called CaretIndex
which "Gets or sets the insertion position index of the caret."
You might also be able to use the value of the SelectionStart
and SelectionLength
properties to find where the caret is, but if the SelectionLength
is larger than zero, that might not work. Try the CaretIndex
property.

Tony Vitabile
- 8,298
- 15
- 67
- 123
-
Thank you, but the position I want is actually vertical offset or (x,y)'s Y value. Because I want to move that textbox up, then I need to know how much vertical offset there is to scroll using Control.ChangeView(null,verticaloffset,null) – litaoshen Dec 17 '15 at 18:32
-
WPF does not work that way, unless you're using a Canvas as your container. You're better off using a `Grid` and changing the `Grid.Row` attached property on each `TextBox`. Then you won't have to worry about coordinates – Tony Vitabile Dec 17 '15 at 19:04
-
0
I did not fully understand your requirement. Currently there is no property/method to get the current caret position in the text. But if you want to move your caret at the start of the textbox than use below code.
txtbox.SelectionStart = 0;
txtbox.SelectionLength = 0;
Edit
By using txtbox SelectionStart and textbox selection length you can get caret position.
int caretIndex = txtbox.SelectionStart + txtbox.SelectionLength;

Muhammad Saifullah
- 4,292
- 1
- 29
- 59
-
I need to get the postion of the caret so that I can scroll to that position. Thanks any way! – litaoshen Dec 17 '15 at 13:57
-
see my edit to get caret position using selectoinstart and selectionLength properties. thanks to @Tony Vitabile :) – Muhammad Saifullah Dec 17 '15 at 17:33
-
Thank you, but the position I want is actually vertical offset or (x,y)'s Y value. – litaoshen Dec 17 '15 at 18:31