Tar
Tar

Reputation: 9025

How to catch Hyperlink click events in the container - loosly coupled way?

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.

Edit - solution, thanks to @nit:

Why Setter Property is not applied on Hyperlink?

Upvotes: 2

Views: 294

Answers (1)

Nitin Purohit
Nitin Purohit

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

Related Questions