加載您的FlowDocument
後,您可以使用LogicalTreeHelper
來枚舉所有UIElements。它可以讓你找到所有的超鏈接。那麼你可以簡單地訂閱他們的MouseEnter
事件。下面是一個代碼:
void SubscribeToAllHyperlinks(object sender, RoutedEventArgs e)
{
var hyperlinks = GetVisuals(this).OfType<Hyperlink>();
foreach (var link in hyperlinks)
link.MouseEnter += Hyperlink_MouseEnter;
}
public static IEnumerable<DependencyObject> GetVisuals(DependencyObject root)
{
foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
{
yield return child;
foreach (var descendants in GetVisuals(child))
yield return descendants;
}
}
private void Hyperlink_MouseEnter(object sender, MouseEventArgs e)
{
// Do whatever you want here
}
我已經與以下XAML測試它:
<FlowDocumentReader>
<FlowDocument>
<Paragraph>
<Hyperlink>asf</Hyperlink>
</Paragraph>
</FlowDocument>
</FlowDocumentReader>
非常感謝您的幫助! – SantiagoARG 2011-03-30 17:52:44