You can simply 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 can 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, because once the dialog is closed, it cannot be reopened, which means that when you click the hyperlink again, an exception will be thrown.
Using interactivity, you can do this without such problems (link to the Blend SDK required):
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<Hyperlink> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <t:CreateDialogAction Type="{x:Type local:Dialogue}"/> </i:EventTrigger> </i:Interaction.Triggers> <Hyperlink.Inlines> <Run Text="Open Dialogue"/> </Hyperlink.Inlines> </Hyperlink>
Action for this:
public class CreateDialogAction : TriggerAction<Hyperlink> { public Type Type { get; set; } protected override void Invoke(object parameter) { if (Type != null && Type.IsSubclassOf(typeof(Window)) && Type.GetConstructor(Type.EmptyTypes) != null) { Window window = Type.GetConstructor(Type.EmptyTypes).Invoke(null) as Window; window.Show(); } } }
HB
source share