Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 649
Iale Avatar asked Mar 10 '11 15:03

Iale


2 Answers

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

like image 137
Rachel Avatar answered Nov 11 '22 12:11

Rachel


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

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

like image 40
jeevan_jk Avatar answered Nov 11 '22 11:11

jeevan_jk