none
Hyperlink in a FlowDocument

    Question

  • I have been trying to develop a help system for my desktop application based on FlowDocuments displayed in a FlowDocumentScrollViewer.  Specifically, I am having a problem with using a Hyperlink to navigate to another document. I have seen several examples of using a Hyperlink to navigate to a web page or to display a Page document, but no use of Hyperlink in a FlowDocument.

    The sample application below displays doc1.xaml with a hyperlink in it. Clicking on the hyperlink does not change the document. What is missing? Is this even possible to do?

    Here is a bare-bones sample:

    Window1.xaml:
    <Window x:Class="HyperlinkSample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
        <Grid>
            <FlowDocumentScrollViewer Name="docViewer" />
        </Grid>
    </Window>

    Window1.xaml.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace HyperlinkSample
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                Uri u = new Uri("doc1.xaml", UriKind.Relative);
                FlowDocument doc = (FlowDocument)Application.LoadComponent(u);
                docViewer.Document = doc;
            }
        }
    }

    doc1.xaml:
    <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        ColumnWidth="400" FontSize="14" FontFamily="Georgia">
        <Paragraph>
            This is a document with a
            <Hyperlink NavigateUri="doc2.xaml">
                link to document2</Hyperlink>.
        </Paragraph>
    </FlowDocument>

    doc2.xaml:
    <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        ColumnWidth="400" FontSize="14" FontFamily="Georgia">
        <Paragraph>
            This is document 2.
        </Paragraph>
    </FlowDocument>

    Tuesday, September 23, 2008 2:26 PM

Answers

  • Hi,

    This is because in a non-navigation Window, there is no NavigationService listening to Hyperlink's RequestNavigate event, so the document won't navigate when clicks on the hyperlinks.

    One approach to navigate the document is to handle the RequestNavigate event to navigate the document manually, something like this:

    [doc1.xaml]

    <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        ColumnWidth="400" FontSize="14" FontFamily="Georgia">

        <Paragraph>

            This is a document with a

            <Hyperlink NavigateUri="doc2.xaml" Name="link1">

                link to document2</Hyperlink> .

        </Paragraph>

    </FlowDocument>

     

    [Window1.xaml.cs]

            void Window1_Loaded(object sender, RoutedEventArgs e)

            {

                Uri u = new Uri("doc1.xaml", UriKind.Relative);

                FlowDocument doc = (FlowDocument)Application.LoadComponent(u);

                docViewer.Document = doc;

     

                Hyperlink link = doc.FindName("link1") as Hyperlink;

                link.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(link_RequestNavigate);

            }

     

            void link_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)

            {

                this.docViewer.Document = (FlowDocument)Application.LoadComponent(e.Uri);

            }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Friday, September 26, 2008 9:04 AM
    Thursday, September 25, 2008 8:13 AM

All replies

  • Hi,

    This is because in a non-navigation Window, there is no NavigationService listening to Hyperlink's RequestNavigate event, so the document won't navigate when clicks on the hyperlinks.

    One approach to navigate the document is to handle the RequestNavigate event to navigate the document manually, something like this:

    [doc1.xaml]

    <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        ColumnWidth="400" FontSize="14" FontFamily="Georgia">

        <Paragraph>

            This is a document with a

            <Hyperlink NavigateUri="doc2.xaml" Name="link1">

                link to document2</Hyperlink> .

        </Paragraph>

    </FlowDocument>

     

    [Window1.xaml.cs]

            void Window1_Loaded(object sender, RoutedEventArgs e)

            {

                Uri u = new Uri("doc1.xaml", UriKind.Relative);

                FlowDocument doc = (FlowDocument)Application.LoadComponent(u);

                docViewer.Document = doc;

     

                Hyperlink link = doc.FindName("link1") as Hyperlink;

                link.RequestNavigate += new System.Windows.Navigation.RequestNavigateEventHandler(link_RequestNavigate);

            }

     

            void link_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)

            {

                this.docViewer.Document = (FlowDocument)Application.LoadComponent(e.Uri);

            }



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Zhi-Xin Ye Friday, September 26, 2008 9:04 AM
    Thursday, September 25, 2008 8:13 AM
  • Thanks for the help. That will do it.
    Wednesday, October 01, 2008 6:11 PM
  • Hi Zhi,

    This is looks like hardcoding link name 

    Hyperlink link = doc.FindName("link1") as Hyperlink;
    and also workss for one link.. the case like  i have multiple heperlink flowdocument page.. if i click any of the link that should navigate to respective uri specified in that link..example

    <Hyperlink TextDecorations="Underline" Foreground="#008000" NavigateUri="D:\project \PLATINUM_web\2.xaml"></Hyperlink>

    in this case if i click the the hyper link it should render 2.xaml flow document in FlowDocumentReader.

    Note: if I open same FlowDocument in IE  it's navigating fine for all the hyperlinks.. Problems comes while I am loading the FlowDocument file in my application in FlowDocumentReader and try to navigate from there.

    Regards
    Mathi

     

      

     

      

     


    Mathi
    Tuesday, June 30, 2009 11:30 AM