Get XY coordinates of TextPointer within WPF Richtextbox
I would like to know if is possible to get XY coordinates of TextPointer with开发者_运维问答in WPF Richtextbox.
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
Rect Example = myRichTextBox.CaretPosition.GetCharacterRect(LogicalDirection.Forward)
This will give the X,Y coordinates of the carete position
精彩评论