开发者

Clickable link inside a text control like RichTextControl in WPF?

开发者 https://www.devze.com 2023-01-27 07:12 出处:网络
I want to be able to have some text clickable like in webpages in WPF. The control should have both non-functional text (for display) both also some of its parts as completely clickable.

I want to be able to have some text clickable like in webpages in WPF. The control should have both non-functional text (for display) both also some of its parts as completely clickable.

Say like Wikipedia.

But this is an independent standalone offline app.

I tried various things but I couldn't do it, especially the clicking doesn't function like web pages, i.e. 1 click to open the url contained within开发者_如何学C the tools.


If you don't have a requirement that it be a full-blown FlowDocument, then you can just use a plain old WPF TextBlock, and put Hyperlinks in it.

<TextBlock>
    Here's some text with a
    <Hyperlink NavigateUri="Page2.xaml">link to another XAML page</Hyperlink>
    and a
    <Hyperlink NavigateUri="http://msdn.microsoft.com/">link to the
    Web</Hyperlink>.
</TextBlock>

If you need scrolling, just put a ScrollViewer around it.

If you need the paginated, multi-column viewer, then you'll need to go with an all-out FlowDocument, but if all you want is text with hyperlinks, TextBlock + Hyperlink should be all you need.


you should try setting the flow document manually and creating hyperlinks within the flow document...

Here is some text taken from the following link: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/99ae9d9c-1dd4-4acd-8d5d-6eb739adeb98

" Hi,

It is possible. Here is a small example of creating hyperlink to paragraph/section/table.

In order to navigate to website, we can create a Frame Control for navigation. The hierarchical relationship of elements in this example is like this :

Frame-->FlowDocument-->Table-->Section-->Paragraph-->Hyperlink

In the code behind:

public Window1()
        {
            InitializeComponent();

            // add a Frame for navigation
            Frame frame = new Frame();
            this.Content = frame;
            //add FlowDocument
            FlowDocument doc = new FlowDocument();
            frame.Navigate(doc);

            //add Table
            Table table = new Table();
            doc.Blocks.Add(table );
            TableRowGroup group = new TableRowGroup();
            table.RowGroups.Add(group );

            TableColumn col1 = new TableColumn();
            TableColumn col2 = new TableColumn();
            table.Columns.Add(col1 );
            table.Columns.Add(col2);

            TableRow row1 = new TableRow();
            TableCell cel1 = new TableCell();
            row1.Cells.Add(cel1);

            group.Rows.Add(row1);

            //add Section
            Section mySection = new Section();
            //add section to the Table cell.
            cel1.Blocks.Add(mySection);

            Paragraph paraValue = new Paragraph();
            Hyperlink hl = new Hyperlink(new Run("Click Here to Google"));
            hl.Foreground = Brushes.Red;
            paraValue.Inlines.Add(hl);

            hl.FontSize = 11;
            hl .NavigateUri =new Uri ("Http://www.google.cn");

            mySection.Blocks.Add(paraValue);
        }

If you have any additional question about this,please feel free to ask.

Thanks. "

0

精彩评论

暂无评论...
验证码 换一张
取 消