WPF TextBox obtain and set caret position from mouse pointer
问题 I am trying to set TextBox caret position according to the mouse coordinates when hovering over the TextBox. I can obtain the mouse coordinates relative to the TextBox by using Mouse.GetPosition(this.MyTextBox) but I need help translating those coordinates into the caret position. Any help would be greatly appreciated! 回答1: MyTextBox.CaptureMouse(); MyTextBox.CaretIndex = MyTextBox.GetCharacterIndexFromPoint(Mouse.GetPosition(MyTextBox), true); 回答2: MyTextBox.GetCharacterIndexFromPoint(..)