Open WPF form when clicking hyperlink on WPF

Hi, I want to open a new WPF form when I click the WPF hyperlink. How can I do it. I have seen many examples that open only the web url, but I want to open a new WPF form.

+7
source share
4 answers

You can achieve this as follows:

<Label Height="25" Margin="26,27,116,0" Name="label1" VerticalAlignment="Top"> <Hyperlink Click="Hyperlink_Click">Click Me</Hyperlink> </Label> 

and handle it like this:

 private void Hyperlink_Click(object sender, RoutedEventArgs e) { Window2 form2 = new Window2(); form2.Show(); } 
+15
source

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(); } } } 
+5
source

XAML:

 <TextBlock Height="23" HorizontalAlignment="Left" Margin="254,130,0,0" Name="textBlock1" VerticalAlignment="Top"> <Hyperlink Click="Hyperlink_Click">Clique Aqui</Hyperlink> </TextBlock> 

CodeBehind:

 private void Hyperlink_Click(object sender, RoutedEventArgs e) { MainWindow m = new MainWindow(); m.Show(); } 

+1
source

And using MVVM you can do in your view

 <Hyperlink NavigateUri="{Binding MyUri}" Command="{Binding OpenHyperlinkCommand}">Link text </Hyperlink> 

and in your ViewModel

 private ICommand _openHyperlinkCommand; public ICommand OpenHyperlinkCommand { get { if (_openHyperlinkCommand == null) _openHyperlinkCommand = new RelayCommand<object>(p => ExecuteHyperlink()); return _openHyperlinkCommand; } } private void ExecuteHyperlink() { //do stuff here } 
0
source

All Articles