C # WPF text with links

I just found a new task for myself: Make a word processor that processes more like the Internet than regular text. Designing a good framework for this is something I can't wait to get started, but I need to know what features the GUI side has (it will probably have a lot of GUI issues).

So, the main thing in which I need some kind of control where I can make parts of my text clickable / mouse.

I am new to WPF and don't know how to do this. Does anyone know how to do this? Any examples? Do you already have control?

Thanks in advance

EDIT:

I found a way to do this with richtextbox:

// Create a FlowDocument to contain content for the RichTextBox. FlowDocument myFlowDoc = new FlowDocument(); // Add paragraphs to the FlowDocument. Hyperlink myLink = new Hyperlink(); myLink.Inlines.Add("hyperlink"); myLink.NavigateUri = new Uri("http://www.stackoverflow.com"); // Create a paragraph and add the Run and hyperlink to it. Paragraph myParagraph = new Paragraph(); myParagraph.Inlines.Add("check this link out: "); myParagraph.Inlines.Add(myLink); myFlowDoc.Blocks.Add(myParagraph); // Add initial content to the RichTextBox. richTextBox1.Document = myFlowDoc; 

Now I get a beautiful hyperlink in my text box ... besides, when I click on it, nothing happens. What am I missing here?

+3
source share
3 answers

You can use the Hyperlink class. This is a FrameworkContentElement, so you can use it in a TextBlock or FlowDocument, or anywhere else where you can embed content.

 <TextBlock> <Run>Text</Run> <Hyperlink NavigateUri="http://stackoverflow.com">with</Hyperlink> <Run>some</Run> <Hyperlink NavigateUri="http://google.com">hyperlinks</Hyperlink> </TextBlock> 

You may want to use RichTextBox as part of your editor. It will host a FlowDocument, which may contain materials such as hyperlinks.


Update. There are two ways to handle clicks on a hyperlink. One of them is to handle the RequestNavigate event. This is a routable event , so you can either attach the handler to the hyperlink itself, or attach it to the element above in the tree, for example, Window or RichTextBox:

 // On a specific Hyperlink myLink.RequestNavigate += new RequestNavigateEventHandler(RequestNavigateHandler); // To handle all Hyperlinks in the RichTextBox richTextBox1.AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(RequestNavigateHandler)); 

Another way is to use commanding by setting Command in a hyperlink to ICommand . The Executed method on ICommand will be called when a hyperlink is clicked.

If you want to launch the browser in the handler, you can pass the URI to Process.Start :

 private void RequestNavigateHandler(object sender, RequestNavigateEventArgs e) { Process.Start(e.Uri.ToString()); } 
+18
source

Please note that you also need to set the following properties in the RichTextBox or the hyperlinks will be disabled and will not trigger events. Without IsReadOnly, you need to click the Ctrl-click hyperlinks, with IsReadOnly they are launched with the usual left-click.

 <RichTextBox IsDocumentEnabled="True" IsReadOnly="True"> 
+3
source

The easiest way is to handle the RequestNavigate event as follows:

 ... myLink.RequestNavigate += HandleRequestNavigate; ... private void HandleRequestNavigate(object sender, RoutedEventArgs e) { var link = (Hyperlink)sender; var uri = link.NavigateUri.ToString(); Process.Start(uri); e.Handled = true; }
... myLink.RequestNavigate += HandleRequestNavigate; ... private void HandleRequestNavigate(object sender, RoutedEventArgs e) { var link = (Hyperlink)sender; var uri = link.NavigateUri.ToString(); Process.Start(uri); e.Handled = true; } 

There are some problems with launching the default browser by passing the url to Process.Start, and you might want to google for a better way to implement the handler.

+1
source

Source: https://habr.com/ru/post/1393886/


All Articles