检查文件末尾是否有插入符号

时间:2015-08-05 17:00:46

标签: wpf document caret

我想检查RichTextBox.CaretPosition是否在文档的末尾。

Image shows problem

我有这段代码。

public void SetFormatInRichTextBox(RichTextBox rich,Color color,string property)
{
        if (IsPositionContainedBetween(rich.CaretPosition, ((Paragraph)rich.Document.Blocks.LastBlock).Inlines.LastInline.ContentStart, ((Paragraph)rich.Document.Blocks.LastBlock).Inlines.LastInline.ContentEnd))
        {

        }
}
public bool IsPositionContainedBetween(TextPointer positionToTest, TextPointer start, TextPointer end)
{

        if (!positionToTest.IsInSameDocument(start) || !positionToTest.IsInSameDocument(end))
            return false;

        return start.CompareTo(positionToTest) <= 0 && positionToTest.CompareTo(end) <= 0;
}

但是,方法IsPositionContainedBetween总是返回true。我使用WPF。

1 个答案:

答案 0 :(得分:1)

我有解决方案

TextRange tr=new TextRange(rich.CaretPosition,rich.CaretPosition.Paragraph.ContentEnd);
            if(tr.IsEmpty)
            {
               //caret is at the end of paragraph
            }