Get XY coordinates of TextPointer in WPF Richtextbox

I would like to know if it is possible to get the XY coordinates of TextPointer in WPF Richtextbox.

+3
source share
2 answers

You can use Mouse.GetPosition(MyRichTextBox)that will return you the X, Y coordinates of the mouse in the RichTextBox

Here is a simple example that I used to validate:

<StackPanel>
    <RichTextBox x:Name="Test" Height="100" Width="100" MouseMove="Test_MouseMove"  />
    <Label x:Name="Test2" Content="{Binding }" />
</StackPanel>

Code for:

private void Test_MouseMove(object sender, MouseEventArgs e)
{
    this.Test2.DataContext = Mouse.GetPosition(this.Test);
}

EDIT

I didn’t understand that you want to get the caret position instead of the mouse position. Use myRichTextBox.CaretPosition.GetCharacterRect(LogicalDirection.Forward)to get carriage X, Y coordinates

+9
source
Rect Example = myRichTextBox.CaretPosition.GetCharacterRect(LogicalDirection.Forward)

This will give the x, y coordinates of the carriage position

+2
source

All Articles