July 8, 2013

WPF Hyperlink Sample

Here are a couple of examples
WPF Application Hyperlink (in this case handle the RequestNavigate)
<TextBlock>Files found using the given <Hyperlink 
Name="hlRules" 
NavigateUri="Rules" 
RequestNavigate="Hyperlink_RequestNavigate">Rules</Hyperlink>:
</TextBlock>
private void HyperlinkRequestNavigate(object sender, RequestNavigateEventArgs e)
{
   // What to do with a hyperlink navigation request?
   // If it is an internet URI this will invoke the default browser
   // Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));

   // Otherwise show a dialog or whatever you want
   e.Handled = true;
}
Also check this out this link for a reusable hyperlink navigation routine using dependent properties.

How about using a WPF hyperlink in MVVM. Here is how it is done.
In the XAML add the textblock with the hyperlink and bind it to a command ('AboutCommand' in this case). Note in this sample the text of the hyperlink is also bound to some property 'Version'
 <TextBlock>
    <Hyperlink Command="{Binding AboutCommand}"><TextBlock Text="{Binding Version}" /></Hyperlink>
 </TextBlock>
The command is implemented in the ViewModel
private RelayCommand aboutCommand;

public RelayCommand AboutCommand
{
 get
 {
  return aboutCommand ?? (aboutCommand = new RelayCommand(ExecuteAboutCommand));
 }
}

private void ExecuteAboutCommand()
{
 IAboutDialogService aboutDialogService = ServiceLocator.Current.GetInstance<IAboutDialogService>();
 aboutDialogService.ShowAboutDialog(this.Title);
}

No comments: