Question

I just found myself a new challenge: Make a Word Processor that is in handling more like the web than plain text. Designing a nice framework for this is what i cant wait to start with, but i do need to know what the possibilities are at the GUI side (it will probably have loads of GUI challenges).

So the basic thing that I need some sort of Control where I can make parts of my text clickable / mouse-over-able.

I'm kinda new to WPF and not sure how to do this. Has anybody an idea how to make this? Are there examples? Are there already controls for this?

Thanks in advance

EDIT:

I found out some way to do it with a 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;

I now get a nice hyperlink in my textbox... except when i click it, nothing happens. what am i missing here?

Was it helpful?

Solution

You can use the Hyperlink class. It's a FrameworkContentElement, so you can use it in a TextBlock or FlowDocument or anywhere else 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 look at using a RichTextBox as part of your editor. This will host a FlowDocument, which can contain content such as Hyperlinks.


Update: There are two ways to handle clicks on the Hyperlink. One is to handle the RequestNavigate event. It is a Routed Event, so you can either attach a handler to the Hyperlink itself or you can attach one to an element higher in the tree such as the Window or the RichTextBox:

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

The other way is to use commanding by setting the Command property on the Hyperlink to an ICommand implementation. The Executed method on the ICommand will be called when the Hyperlink is clicked.

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

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

OTHER TIPS

Note you also need to set the following properties on your RichTextBox or the hyperlinks will be disabled and won't fire off events. Without IsReadOnly you need to Ctrl-click the hyperlinks, with IsReadOnly they fire with a regular left-click.

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

The simplest way is to handle RequestNavigate event like this:


...
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 issues with starting a default browser by passing url to the Process.Start and you might want to google for a better way to implement the handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top