我想知道是否可以在WPF Richtextbox中获取TextPointer的XY坐标。
最佳答案
您可以使用Mouse.GetPosition(MyRichTextBox)
,它将在RichTextBox中返回鼠标的X,Y坐标
这是我用来验证其正确性的一个简单示例:
<StackPanel>
<RichTextBox x:Name="Test" Height="100" Width="100" MouseMove="Test_MouseMove" />
<Label x:Name="Test2" Content="{Binding }" />
</StackPanel>
背后的代码:
private void Test_MouseMove(object sender, MouseEventArgs e)
{
this.Test2.DataContext = Mouse.GetPosition(this.Test);
}
编辑
没意识到您想获得插入符号位置而不是鼠标位置。使用
myRichTextBox.CaretPosition.GetCharacterRect(LogicalDirection.Forward)
获取插入符的X,Y坐标
关于wpf - 在WPF Richtextbox中获取TextPointer的XY坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5261629/