Clicking HyperLinks in a RichTextBox without holding down CTRL - WPF

后端 未结 6 1091
鱼传尺愫
鱼传尺愫 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 11:56

    JHubbard80's answer is a possible solution, it's the easiest way if you do not need the content to be selected.

    However I need that :P here is my approach: set a style for the Hyperlinks inside the RichTextBox. The essential is to use a EventSetter to make the Hyperlinks handling the MouseLeftButtonDown event.

    
        
            
        
    
    

    And in codebehind:

    private void Hyperlink_MouseLeftButtonDown(object sender, MouseEventArgs e)
    {
        var hyperlink = (Hyperlink)sender;
        Process.Start(hyperlink.NavigateUri.ToString());
    }
    

    Thanks to gcores for the inspiaration.

提交回复
热议问题