如何确定 WPF 中超链接的坐标
我有一个带有 FlowDocument 的 WPF 窗口,其中包含多个超链接:
<FlowDocumentScrollViewer>
<FlowDocument TextAlignment="Left" >
<Paragraph>Some text here
<Hyperlink Click="Hyperlink_Click">open form</Hyperlink>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
在 C# 代码中,我处理 Click 事件来创建并显示一个新的 WPF 窗口:
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
if (sender is Hyperlink)
{
var wnd = new SomeWindow();
//wnd.Left = ???
//wnd.Top = ???
wnd.Show();
}
}
我需要此窗口出现在超链接的实际位置旁边。所以我假设它需要为窗口的 Left 和 Top 属性赋值。但我不知道如何获得超链接位置。
I have WPF window with a FlowDocument with several hyperlinks in it:
<FlowDocumentScrollViewer>
<FlowDocument TextAlignment="Left" >
<Paragraph>Some text here
<Hyperlink Click="Hyperlink_Click">open form</Hyperlink>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
In the C# code I handle Click event to create and show a new WPF Window:
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
if (sender is Hyperlink)
{
var wnd = new SomeWindow();
//wnd.Left = ???
//wnd.Top = ???
wnd.Show();
}
}
I need this window to appear next to hyperlink's actual position. So I assume it requires assigning values to the window's Left and Top properties. But I have no idea how to obtain hyperlink position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ContentStart 或 < a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.textelement.contentend.aspx" rel="nofollow noreferrer">ContentEnd 获取 TextPointer超链接的开头或结尾,然后调用 GetCharacterRect 获取相对于 FlowDocumentScrollViewer 的边界框。如果您获得对 FlowDocumentScrollViewer 的引用,则可以使用 PointToScreen 将其转换为屏幕坐标。
You can use ContentStart or ContentEnd to get a TextPointer for the start or end of the hyperlink and then call GetCharacterRect to get the bounding box relative to the FlowDocumentScrollViewer. If you get a reference to the FlowDocumentScrollViewer, you can use PointToScreen to convert it to screen coordinates.