Pressing HyperLinks in RichTextBox without holding CTRL - WPF

I have a WPF RichTextBox with isReadOnly set to True . I would like users to be able to click on the HyperLinks contained in the RichTextBox without having to hold Ctrl .

The Click event on HyperLink does not seem to fire if Ctrl is not held, so I'm not sure how to proceed.

+10
hyperlink click wpf richtextbox ctrl
Apr 17 '09 at 21:28
source share
5 answers

I managed to find a way around this, largely by accident.

Content uploaded to my RichTextBox is simply saved (or entered) as a simple string. I have subclassed RichTextBox to allow Document properties to be bound to it.

What is relevant to the question is that I have an IValueConverter Convert () overload that looks something like this (code that is not essential for the solution has been removed):

 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 gives me the behavior I want (dragging and dropping simple lines into a RichTextBox and getting formatting), and also leads to links that behave like a regular link, rather than the one embedded in a Word document.

+3
Apr 17 '09 at 23:18
source share

I have found a solution. Set IsDocumentEnabled to True and set IsReadOnly to True.

 <RichTextBox IsReadOnly="True" IsDocumentEnabled="True" /> 

Once I have done this, the mouse will turn into a β€œhand” when I attach the text displayed in the HyperLink tag. Pressing the button without saving will trigger the 'Click' event.

I am using WPF from .NET 4. I do not know if earlier versions of .NET work as described above.

+20
May 26 '11 at 23:55
source share

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

However, I need this: P here is my approach: set the style for Hyperlink inside the RichTextBox . It is essential to use an EventSetter to make Hyperlink handle the MouseLeftButtonDown event.

 <RichTextBox> <RichTextBox.Resources> <Style TargetType="Hyperlink"> <Setter Property="Cursor" Value="Hand" /> <EventSetter Event="MouseLeftButtonDown" Handler="Hyperlink_MouseLeftButtonDown" /> </Style> </RichTextBox.Resources> </RichTextBox> 

And in codebehind:

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

Thanks to gcores for inspiration.

+11
Nov 24 '13 at 18:42
source share

Have you tried to handle the MouseLeftButtonDown event instead of the Click event?

0
Apr 17 '09 at 22:52
source share

I changed EventSetter from @hillin's answer. MouseLeftButtonDown did not work in my code (.Net Framework 4.5.2).

 <EventSetter Event="RequestNavigate" Handler="Hyperlink_RequestNavigate" /> 
 private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) { Process.Start(e.Uri.ToString()); } 
0
Feb 01 '19 at 2:39
source share



All Articles