如何在RichTextBox中获得选择的方向?

时间:2019-04-26 08:20:36

标签: c# wpf richtextbox

RichTextBox中的文本,用户可以向前或向后选择文本。如何以编程方式确定选择的方向? (LogicalDirectionEnd中的Start并不表示这一点)

1 个答案:

答案 0 :(得分:2)

您可以将CaretPosition与当前Selection进行比较,以确定从哪个方向选择文本:

TextPointer caretPos = richTextBox.CaretPosition;
TextPointer selectStart = richTextBox.Selection.Start;
TextPointer selectEnd = richTextBox.Selection.End;

if(caretPos.CompareTo(selectStart) == 0)
{
    //The text was selected from right to left.
}
else if(caretPos.CompareTo(selectEnd) == 0)
{
    //The text was selected from left to right.
}