Iale
Iale

Reputation: 678

Get XY coordinates of TextPointer within WPF Richtextbox

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

Upvotes: 6

Views: 3579

Answers (2)

jeevan_jk
jeevan_jk

Reputation: 121

Rect Example = myRichTextBox.CaretPosition.GetCharacterRect(LogicalDirection.Forward)

This will give the X,Y coordinates of the carete position

Upvotes: 4

Rachel
Rachel

Reputation: 132558

You can use Mouse.GetPosition(MyRichTextBox) which will return you the X,Y coordinates of the Mouse within the RichTextBox

Here's a simple example that I used to verify this was correct:

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

Code Behind:

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

EDIT

Didn't realize you wanted to get caret position instead of mouse position. Use myRichTextBox.CaretPosition.GetCharacterRect(LogicalDirection.Forward) to get the X,Y coordinates of the caret

Upvotes: 10

Related Questions