WPF TextBox obtain and set caret position from mouse pointer

筅森魡賤 提交于 2019-12-22 11:34:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!