views:

46

answers:

2

Hi all,

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?

+1  A: 

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"&gt;with&lt;/Hyperlink&gt;
    <Run>some</Run>
    <Hyperlink NavigateUri="http://google.com"&gt;hyperlinks&lt;/Hyperlink&gt;
</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());
}
Quartermeister
check my edit in 5 mins, thanks!
SirLenz0rlot
this should do the trick, thanks a lot!
SirLenz0rlot
+1  A: 

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.

Konstantin Oznobihin