0

I have looked at various solutions on how to have a simple URL on a textfield, a rich text field in a WPF implementation using C#.

Tried to implement solutions on Clicking HyperLinks in a RichTextBox without holding down CTRL - WPF and Add clickable hyperlinks to a RichTextBox without new paragraph. It just looks overly complicated for what i think should be a simple task.

<RichTextBox IsReadOnly="True" IsDocumentEnabled="True"> <FlowDocument> <Paragraph FontSize="12"> See www.google.com</Paragraph> </FlowDocument> </RichTextBox> 

I also tried the implementation on the link Example using Hyperlink in WPF

Here, the error I get is this. MainWindow does not contain a definition of Hyperlink_RequestNAvigate and no accessible extension method hyperlink_RequestNavigate accepting a first argument of type MainWindow could be found, are you missing a using directive of an assembly reference.

<TextBlock> <Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate"> Click here </Hyperlink> </TextBlock> 

code behind

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) { // for .NET Core you need to add UseShellExecute = true // see https://learn.microsoft.com/dotnet/api/system.diagnostics.processstartinfo.useshellexecute#property-value Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); e.Handled = true; } 

I am looking to have a text saying for more information click here and if the user clicks on click here, they navigate to a predefined url.

1
  • Your code seems a 1:1 copy of the other SO question concerning that subject that you have linked: stackoverflow.com/questions/10238694/… The error message indicates that you didn't put that event handler Hyperlink_RequestNavigate into file MainWindow.xaml.cs but rather in some other cs file... Commented Aug 4, 2021 at 23:14

1 Answer 1

0

The way I've implemented hyperlinks in the past is to create a attached property, which you can add onto the Hyperlink, which will handle the RequestNavigate itself.

For example:

namespace WpfDemo.Extensions { public static class HyperlinkExtensions { public static bool GetIsExternal(DependencyObject obj) { return (bool)obj.GetValue(IsExternalProperty); } public static void SetIsExternal(DependencyObject obj, bool value) { obj.SetValue(IsExternalProperty, value); } public static readonly DependencyProperty IsExternalProperty = DependencyProperty.RegisterAttached("IsExternal", typeof(bool), typeof(HyperlinkExtensions), new UIPropertyMetadata(false, OnIsExternalChanged)); private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args) { var hyperlink = sender as Hyperlink; if ((bool)args.NewValue) hyperlink.RequestNavigate += Hyperlink_RequestNavigate; else hyperlink.RequestNavigate -= Hyperlink_RequestNavigate; } private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) { ProcessStartInfo psi = new() { FileName = e.Uri.AbsoluteUri, UseShellExecute = true, }; Process.Start(psi); e.Handled = true; } } } 

The code defines an attached property that when you add to a Hyperlink, will handle the logic for navigating to the links for you.

You can use the following code the following way:

<Window x:Class="WpfDemo.MainWindow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:helper="clr-namespace:WpfDemo.Extensions" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Grid> <TextBlock> <Hyperlink helper:HyperlinkExtensions.IsExternal="true" NavigateUri="https://google.com"> Google Link </Hyperlink> </TextBlock> </Grid> </Window> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, I get 2 errors. The type or namespace name Hyperlink could not be found (are you missing a using directive or assembly reference? The second error is ProcessStartInfo does not contain a definition for filename. XAML also coming up as invalid, the attachable propety IsExternal was not found in the type HyperLinkExternsions.
@learner You will need to modify the namespaces to match your solution (in both the XAML and the class). For the second error, you can use the original code that you have in Hyperlink_RequestNavigate (the code I posted was for .NET Core).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.