Get the caret position when losing focus in WPF
- by DrLazer
I have two textbox's. I have an event setup for the "onLostFocus" of the textbox's. If i finish typing a word into both boxes one after the other all is well. At the point where i click back on the first textbox i want to click halfway through the word (perfectly resonable thing for a user to do). When the onLostFocus event fires here is my code :
void tbox_LostFocus(object sender, RoutedEventArgs e)
{
IInputElement focusedelement = FocusManager.GetFocusedElement(this);
if (focusedelement is TextBox)
{
TextBox focusedTextBox = focusedelement as TextBox;
int focusIndex = m_commandsStacker.Children.IndexOf(focusedTextBox);
int caretIndex = focusedTextBox.CaretIndex;
The caret index is 0. At that point I refresh the whole form and set parameters and all other kinds of whizzery, storing the texbox index and caret position to put everything back the same. Why does it return 0 and not the position where the caret should be halfway between the word?