问题
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(..) this returns the zero based index of the character that is closest to the specified point. please see the link below: http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.getcharacterindexfrompoint.aspx
来源:https://stackoverflow.com/questions/10609933/wpf-textbox-obtain-and-set-caret-position-from-mouse-pointer