开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜