0

UWP 的 Richeditbox 中似乎缺少 GetCharIndexFromPosition。当某个范围悬停在 RichEditBox 中时,我想显示一个工具提示。UWP可以做到这一点吗?

4

1 回答 1

0

在 UWP 中,我们可以使用GetRangeFromPoint(Point, PointOptions)方法作为GetCharIndexFromPosition. 此方法检索屏幕上特定点处或最接近的退化(空)文本范围。它返回一个ITextRange对象,其StartPosition属性ITextRange类似于方法返回的字符索引GetCharIndexFromPosition

以下是一个简单的示例:

XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <RichEditBox x:Name="editor" />
</Grid>

代码隐藏:

public MainPage()
{
    this.InitializeComponent();
    editor.Document.SetText(Windows.UI.Text.TextSetOptions.None, @"This is a text for testing.");
    editor.AddHandler(PointerMovedEvent, new PointerEventHandler(editor_PointerMoved), true);
}

private void editor_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    var position = e.GetCurrentPoint(editor).Position;

    var range = editor.Document.GetRangeFromPoint(position, Windows.UI.Text.PointOptions.ClientCoordinates);

    System.Diagnostics.Debug.WriteLine(range.StartPosition);
}
于 2017-03-29T13:13:25.633 回答