【发布时间】:2010-08-04 16:13:50
【问题描述】:
在 Winforms RichTextBox 控件中,我之前使用了 GetLineFromCharIndex 方法和 GetFirstCharIndexOfCurrentLine 来计算当前行上键入文本的起点和终点。
我正在努力使用 Silverlight 4 中的新 RichTextBox 控件,因为似乎没有等效的方法。 GetPositionFromPoint 可用,但看起来有点笨重。
干杯。
已更新...我已经设法使这项工作但这需要我使用控件的Select方法,这感觉很不对...
private string GetCurrentLine()
{
TextPointer prevSelStart = richTextBox1.Selection.Start;
Point lineStart = new Point(0, prevSelStart.GetCharacterRect(LogicalDirection.Forward).Y);
TextPointer prevSelEnd = richTextBox1.Selection.End;
TextPointer currentLineStart = richTextBox1.GetPositionFromPoint(lineStart);
//need to find a way to get the text between two textpointers
//other than performing a temporary selection in the rtb
richTextBox1.Selection.Select(currentLineStart, prevSelStart);
string text = richTextBox1.Selection.Text;
//revert back to previous selection
richTextBox1.Selection.Select(prevSelStart, prevSelEnd);
return text;
}
【问题讨论】:
标签: silverlight-4.0 richtextbox