-1

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.

litaoshen
  • 1,762
  • 1
  • 20
  • 36

2 Answers2

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
  • OK, I see. Thank you so much! – litaoshen Dec 17 '15 at 20:16
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