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 
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'
    <Hyperlink Command="{Binding AboutCommand}"><TextBlock Text="{Binding Version}" /></Hyperlink>
The command is implemented in the ViewModel
private RelayCommand aboutCommand;

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

private void ExecuteAboutCommand()
 IAboutDialogService aboutDialogService = ServiceLocator.Current.GetInstance<IAboutDialogService>();

No comments: