如何获取 TextBox 控件可见客户区域中的插入符位置 (x, y)?我需要向文本框添加自动完成功能。
我发现了WPF 的解决方案 https://stackoverflow.com/questions/1053539/finding-the-position-of-the-caret-in-a-textbox,但不能在Silverlight中应用。
public class AutoCompleteTextBox : TextBox
{
public Point GetPositionFromCharacterIndex(int index)
{
if (TextWrapping == TextWrapping.Wrap) throw new NotSupportedException();
var text = Text.Substring(0, index);
int lastNewLineIndex = text.LastIndexOf('\r');
var leftText = lastNewLineIndex != -1 ? text.Substring(lastNewLineIndex + 1) : text;
var block = new TextBlock
{
FontFamily = FontFamily,
FontSize = FontSize,
FontStretch = FontStretch,
FontStyle = FontStyle,
FontWeight = FontWeight
};
block.Text = text;
double y = block.ActualHeight;
block.Text = leftText;
double x = block.ActualWidth;
var scrollViewer = GetTemplateChild("ContentElement") as ScrollViewer;
var point = scrollViewer != null
? new Point(x - scrollViewer.HorizontalOffset, y - scrollViewer.VerticalOffset)
: new Point(x, y);
point.X += BorderThickness.Left + Padding.Left;
point.Y += BorderThickness.Top + Padding.Top;
return point;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)