string error = null;
this.xamRichTextEditor1.Document.InsertHyperlink(0,
"http://ko.infragistics.com/", out error, "Infragistics",
"Link to the Infragistics website.");
This topic provides information on how to create hyperlinks using the xamRichTextEditor ™ from the developer’s perspective.
The following topics are prerequisites to understanding this topic:
This topic contains the following sections:
The xamRichTextEditor supports internal hyperlinks within the document navigating the user to a website. Hyperlinks are rendered with a different style so they can easily be distinguished from the text amongst the other rich content.
You can use the TrackHistory property to enable a link to be rendered in a different color when it is once visited.
The user may visit a hyperlink by clicking on it while holding the control key pressed. This will trigger a HyperlinkExecuting event, which can be used to inspect and/or modify the URI address or cancel the hyperlink opening.
There are three ways for adding a hyperlink in the RichTextDocument using code. Further details are available after the table.
By invoking the InsertHyperlink method, you can insert a hyperlink at any offset of the document.
The following table maps the desired configuration to its respective property settings.
The code snippet below demonstrates how to create a hyperlink using the InserHyperlink
method:
In C#:
string error = null;
this.xamRichTextEditor1.Document.InsertHyperlink(0,
"http://ko.infragistics.com/", out error, "Infragistics",
"Link to the Infragistics website.");
In Visual Basic:
Dim [error] As String = Nothing
Me.xamRichTextEditor1.Document.InsertHyperlink(0, _
"http://ko.infragistics.com/", _
[error], _
"Infragistics", _
"Link to the Infragistics website.")
By invoking the MakeHyperlink method, you can insert a hyperlink using an existing document span by providing the document span which will be used. This case uses the content of the document as display text, the target URI and the tooltip will be provided as arguments to the method.
The following table maps the desired configuration to its respective property settings.
The code snippet below demonstrates how to create a hyperlink by invoking the MakeHyperlink
method using the current active view selection as link content:
In C#:
string error = null;
this.xamRichTextEditor1.Document.MaketHyperlink(
this.xamRichTextEditor1.ActiveDocumentView.Selection.DocumentSpan,
"http://www.example.com/", out error, "Example.com"
);
In Visual Basic:
Dim [error] As String = Nothing
Me.xamRichTextEditor1.Document.MaketHyperlink( _
Me.xamRichTextEditor1.ActiveDocumentView.Selection.DocumentSpan, _
"http://www.example.com/", [error], "Example.com")
You can also create a hyperlink by creating a HyperlinkNode instance.
The code snippet below demonstrates how to create a hyperlink and add it in the a new paragraph at the end of the document’s content:
In C#:
var hNode = new HyperlinkNode();
hNode.Tooltip = "Link to the Infragistics website.";
hNode.Uri = "http://ko.infragistics.com/";
hNode.SetText("Infragistics");
hNode.TrackHistory = true;
var pNode = new ParagraphNode();
pNode.ChildNodes.Add(hNode);
this.xamRichTextEditor1.Document.RootNode.Body.ChildNodes.Add(pNode);
In Visual Basic:
Dim hNode = New HyperlinkNode()
hNode.Tooltip = "Link to the Infragistics website."
hNode.Uri = "http://ko.infragistics.com/"
hNode.SetText("Infragistics")
hNode.TrackHistory = True
Dim pNode = New ParagraphNode()
pNode.ChildNodes.Add(hNode)
Me.xamRichTextEditor1.Document.RootNode.Body.ChildNodes.Add(pNode)
In C#:
this.xamRichTextEditor.Document.RootNode.Styles.Add(
this.xamRichTextEditor.Document.AvailableStyles["Hyperlink"].Clone()
as CharacterStyle);
In Visual Basic:
Me.xamRichTextEditor.Document.RootNode.Styles.Add(_
TryCast(Me.xamRichTextEditor.Document.AvailableStyles("Hyperlink").Clone(), _
CharacterStyle))
To avoid this you can use the InsertHyperlink
or MakeHyperlink
methods.
When the user clicks a link while holding the control key, the HyperlinkExecuting
event is fired. You can perform the follwoing operation in the event handler using the provided argument of type HyperlinkExecutingEventArgs:
The following table maps the desired operation to its respective property:
The following topics provide additional information related to this topic.