Alex Rivera | Logout

Open WPF form when clicking WPF hyperlink

Asked 2011-04-18T18:30:42.920
16

I want to open a new WPF form when I click in a WPF hyperlink. I've seen a lot of examples that only opens web url, but I want to open a new WPF form.

Edit
Report

1 Answer

5

You could just handle the click event:

<Hyperlink Click="Hyperlink_Click">Link</Hyperlink>
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
    Dialogue diag = new Dialogue();
    diag.Show();
}

You could also go crazy with XAML:

<Hyperlink>
    <Hyperlink.Style>
        <Style TargetType="{x:Type Hyperlink}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Hyperlink.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                <Storyboard.Target>
                                    <local:Dialogue />
                                </Storyboard.Target>
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Hyperlink.Style>
    <Hyperlink.Inlines>
        <Run Text="Open Dialogue"/>
    </Hyperlink.Inlines>
</Hyperlink>

This however is very problematic since once the dialogue is closed it cannot be reopened, that means when you click the hypelink again an exception will be thrown.


Using interactivity you could do this without such problems (needs a reference to the Blend SDK though):

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<Hyperlink>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <t:Creat
answered 2011-04-18T18:43:01.377

Your Answer