Silverlight 4.0 如何从Silverlight 4 RichTextBox控件将当前行文本转换为光标

Silverlight 4.0 如何从Silverlight 4 RichTextBox控件将当前行文本转换为光标,silverlight-4.0,richtextbox,Silverlight 4.0,Richtextbox,在Winforms RichTextBox控件中,我以前使用过GetLineFromCharIndex方法和GetFirstCharIndexOfcCurrentLine计算当前行上键入文本的起点和终点 在Silverlight 4中,我正在努力使用新的RichTextBox控件,因为似乎没有等效的方法GetPositionFromPoint是可用的,但看起来很笨重 干杯 更新…我已经做了一些工作,但这需要我使用控制的选择方法,这感觉非常错误 private string GetCurrentL

在Winforms RichTextBox控件中,我以前使用过GetLineFromCharIndex方法和GetFirstCharIndexOfcCurrentLine计算当前行上键入文本的起点和终点

在Silverlight 4中,我正在努力使用新的RichTextBox控件,因为似乎没有等效的方法GetPositionFromPoint是可用的,但看起来很笨重

干杯

更新…我已经做了一些工作,但这需要我使用控制的选择方法,这感觉非常错误

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;
}

我不认为你不能避免选择,这是一种正确的方法(“选择”只是一种逻辑选择),但是你可以用
textpoint.GetNextInsertionPosition(逻辑方向)
:从
richTextBox1.selection.Start
开始,然后移到行的开头(char!='\n')

我需要弄清楚我是在RTB的顶行还是底行。为此,我使用GetCharacterRect方法,然后比较顶行,看看它是在最后一行还是第一行

您也可以这样做,并使用文本指针在文本中移动,以确定顶部不匹配的次数

下面是查看光标是否位于第一行或最后一行的代码:

    private bool IsCursorOnFirstLine()
    {
        TextPointer contentStart = this.ContentStart;
        TextPointer selection = this.Selection.End;
        Rect startRect = contentStart.GetCharacterRect(LogicalDirection.Forward);
        Rect endRect = selection.GetCharacterRect(LogicalDirection.Forward);
        return startRect.Top == endRect.Top;
    }

    private bool IsCursorOnLastLine()
    {
        TextPointer start = this.Selection.Start;
        TextPointer end = this.ContentEnd;
        Rect startRect = start.GetCharacterRect(LogicalDirection.Forward);
        Rect endRect = end.GetCharacterRect(LogicalDirection.Backward);
        return startRect.Top == endRect.Top;
    }

我对这种方法的担心是,它会人工触发SelectionChanged事件两次。为了克服这一问题,我需要暂时取消订阅事件订阅者。丑陋。