System.Windows.Documents Na ...


.NET Framework Class Library
Hyperlink Class

Updated: February 2009

An inline-level flow content element that provides facilities for hosting hyperlinks within flow content.

Namespace:  System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax

Visual Basic (Declaration)
<UIPermissionAttribute(SecurityAction.InheritanceDemand, Unrestricted := True)> _
Public Class Hyperlink _
    Inherits Span _
    Implements ICommandSource, IUriContext
Visual Basic (Usage)
Dim instance As Hyperlink
C#
[UIPermissionAttribute(SecurityAction.InheritanceDemand, Unrestricted = true)]
public class Hyperlink : Span, ICommandSource, 
    IUriContext
Visual C++
[UIPermissionAttribute(SecurityAction::InheritanceDemand, Unrestricted = true)]
public ref class Hyperlink : public Span, 
    ICommandSource, IUriContext
JScript
public class Hyperlink extends Span implements ICommandSource, IUriContext
XAML Object Element Usage
<Hyperlink>
  Inlines
</Hyperlink>
Remarks

Hyperlink implements the NavigateUri property that you set with the Uri of the content that should be navigated to when the Hyperlink is clicked. Hyperlink navigation can only occur, however, if either the direct or indirect parent of a Hyperlink is a navigation host, including NavigationWindow, Frame, or any browser that can host XBAPs (which includes Internet Explorer 7, Microsoft Internet Explorer 6, and Firefox 2.0+). For more information, see the Navigation Hosts topic in Navigation Overview.

Content Model: Hyperlink enforces a strong content model for child content. See TextElement Content Model Overview for more information about the Hyperlink content model.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.

Examples

The following example shows simple use of a Hyperlink element.

XAML
<Paragraph>
  <Run>Text preceding the hyperlink.</Run>
  <Hyperlink
    NavigateUri="http://search.msn.com"
  >
    Link text.
  </Hyperlink>
  <Run Name="test">Text following the hyperlink.</Run>
</Paragraph>

The following example shows how to create a Hyperlink programmatically.

C#
Paragraph parx = new Paragraph();
Run run1 = new Run("Text preceeding the hyperlink.");
Run run2 = new Run("Text following the hyperlink.");
Run run3 = new Run("Link Text.");

Hyperlink hyperl = new Hyperlink(run3);
hyperl.NavigateUri = new Uri("http://search.msn.com");

parx.Inlines.Add(run1);
parx.Inlines.Add(hyperl);
parx.Inlines.Add(run2);
Inheritance Hierarchy

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows..::.ContentElement
        System.Windows..::.FrameworkContentElement
          System.Windows.Documents..::.TextElement
            System.Windows.Documents..::.Inline
              System.Windows.Documents..::.Span
                System.Windows.Documents..::.Hyperlink
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Change History

Date

History

Reason

February 2009

Described how default styles change dependency properties.

Customer feedback.

Tags :


Community Content

Dave Sexton
Open in Web Browser

When an end-user clicks a Hyperlink in a WPF application it does not open a web browser to the value specified in the NavigateUri property. For this to occur, it is the developer's responsibility to add an event handler for the Hyperlink.RequestNavigate event and then launch a browser to the desired URI.

This functionality can be encapsulated in a control by subclassing Hyperlink, as shown in the following C# example.

public sealed class ExternalHyperlink : Hyperlink
{
  public ExternalHyperlink()
  {
    this.RequestNavigate += NavigateByDefaultProcess;
  }
 
  private void NavigateByDefaultProcess(object sender, RequestNavigateEventArgs e)
  {
    Uri uri = this.NavigateUri;
 
    if (uri != null)
    {
      if (!uri.IsAbsoluteUri)
        throw new InvalidOperationException("An absolute URI is required.");
 
      System.Diagnostics.Process.Start(uri.ToString());
    }
  }
}

To use this control in XAML you must first assign an XML prefix to the control's namespace using the xmlns attribute on the root element of your XAML file. In the following example, it is assumed that the control has been defined within the MyControlsNamespace namespace of an assembly named, MyControls.

<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mycontrols="clr-namespace:MyControlsNamespace;assembly=MyControls"
Title="My App">
...
</Window>

Then you can use the ExternalHyperlink control in place of the Hyperlink control.

  
<mycontrols:ExternalHyperlink NavigateUri="http://www.wikipedia.org">Wiki info</mycontrols:ExternalHyperlink>

Tags :

Page view tracker