0

I use the following code to add Hyperlink to a RichTextBox. However, I don't want to let the user change the Hyperlink name, after inserting it. I want to make the Hyperlink non-editable. So when user tries to edit it, I want to delete the Hyperlink or convert it to plain text.

    protected void AddHyperlink(string linkURL, string linkName) {
      Paragraph para = new Paragraph();

      Hyperlink link = new Hyperlink(new Run(""), rtb.CaretPosition);
      link.IsEnabled = true;
      link.Inlines.Add(linkName);
      link.Tag = "supername";
      link.NavigateUri = new Uri(linkURL);
      link.RequestNavigate += Link_RequestNavigate;
      link.TextDecorations = null;
    }

enter image description here

slugster
  • 49,403
  • 14
  • 95
  • 145

1 Answers1

0

Welcome to SO!

Try wrapping the hyperlink in a label:

Paragraph para = new Paragraph();

Hyperlink link = new Hyperlink();
link.IsEnabled = true;
link.Inlines.Add(linkName);
link.NavigateUri = new Uri(linkURL);
link.RequestNavigate += Link_RequestNavigate;

var label = new Label { Content = link, Padding=new Thickness(0) };
para.Inlines.Add(label);

rtb.Document.Blocks.Add(para);
Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
  • Thank you, that takes care of it, but hyperlink is not working anymore when I click on it and if I try to delete label, it throws an error "Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. " –  undomanager Jun 10 '20 at 02:13
  • By the way, in my program later I use the paragraph object to parse for items.According to this I have to keep it as Hyperlink. https://learn.microsoft.com/en-us/dotnet/api/system.windows.documents.paragraph?view=netcore-3.1 –  undomanager Jun 10 '20 at 03:11
  • I think there should be a setting on Hyperlink itself to make it readonly, but I can't find it –  undomanager Jun 10 '20 at 03:12