Clicking HyperLinks in a RichTextBox without holding down CTRL - WPF

后端 未结 6 1093
鱼传尺愫
鱼传尺愫 2020-11-30 11:35

I have a WPF RichTextBox with isReadOnly set to True. I would like users to be able to click on HyperLinks contained within the RichTextBox, withou

6条回答
  •  失恋的感觉
    2020-11-30 12:02

    Managed to find a way around this, pretty much by accident.

    The content that's loaded into my RichTextBox is just stored (or inputted) as a plain string. I have subclassed the RichTextBox to allow binding against it's Document property.

    What's relevant to the question, is that I have an IValueConverter Convert() overload that looks something like this (code non-essential to the solution has been stripped out):

    FlowDocument doc = new FlowDocument();
    Paragraph graph = new Paragraph();
    
    Hyperlink textLink = new Hyperlink(new Run(textSplit));
    textLink.NavigateUri = new Uri(textSplit);
    textLink.RequestNavigate += 
      new System.Windows.Navigation.RequestNavigateEventHandler(navHandler);
    
    graph.Inlines.Add(textLink);
    graph.Inlines.Add(new Run(nonLinkStrings));
    
    doc.Blocks.Add(graph);
    
    return doc;
    

    This gets me the behavior I want (shoving plain strings into RichTextBox and getting formatting) and it also results in links that behave like a normal link, rather than one that's embedded in a Word document.

提交回复
热议问题