【问题标题】:WPF TextBox obtain and set caret position from mouse pointerWPF TextBox 从鼠标指针获取并设置插入符号位置
【发布时间】:2012-12-11 02:26:14
【问题描述】:

当鼠标悬停在文本框上时,我试图根据鼠标坐标设置文本框插入符号的位置。 我可以使用 Mouse.GetPosition(this.MyTextBox) 获得相对于 TextBox 的鼠标坐标,但我需要帮助将这些坐标转换为插入符号位置。 任何帮助将不胜感激!

【问题讨论】:

  • 我认为您应该尝试在 TextBox 中模拟鼠标单击事件并将鼠标位置作为事件参数传递
  • 在相关问题中查看this answer

标签: wpf textbox mouse coordinates caret


【解决方案1】:
MyTextBox.CaptureMouse();
MyTextBox.CaretIndex = MyTextBox.GetCharacterIndexFromPoint(Mouse.GetPosition(MyTextBox), true);

【讨论】:

    【解决方案2】:

    MyTextBox.GetCharacterIndexFromPoint(..) 这将返回最接近指定点的字符的从零开始的索引。 请看下面的链接: http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.getcharacterindexfrompoint.aspx

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      相关资源
      最近更新 更多