Reputation: 9025
For start - this question is exactly what I want to achieve, but without the run-time overhead of walking each Hyperlink
.
Is there a way to catch the Click
event of Hyperlink
in higher level (e.g. in Window
) ?
PreviewMouseDown
event gives me the Run
element in which the Hyperlink
located (via e.Source
or e.OriginalSource
), not the Hyperlink
itself.
Why Setter Property is not applied on Hyperlink?
Upvotes: 2
Views: 294
Reputation: 18578
I think this is what you can do in PreviewMouseDown event handler:
private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Run r = e.OriginalSource as Run;
if(r != null)
{
Hyperlink hyperlink = r.Parent as Hyperlink;
if(hyperlink != null)
{
//Your code here
}
}
}
}
Or if you want that all the hyperlink click should be redirected to same command handler in your ViewModel you can add style to do this as follows:
<Style TargetType="{x:Type Hyperlink}">
<Setter Property="Command" Value="{Binding MyCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
<Setter Property="CommandParameter" Value="{Binding}"/>
</Style>
thanks
Upvotes: 3