WPF中RichTextControl之类的文本控件中的可点击链接?

时间:2010-11-30 00:30:01

标签: c# .net wpf hyperlink linklabel

我希望能够像WPF中的网页一样点击一些文字。控件应该同时具有非功能性文本(用于显示)以及其部分可完全可点击的部分。

像维基百科一样说。

但这是一个独立的独立离线应用程序。

我尝试了各种各样的东西,但我无法做到,特别是点击功能不像网页,即点击一下即可打开工具中包含的网址。

2 个答案:

答案 0 :(得分:1)

您应该尝试手动设置流文档并在流文档中创建超链接......

以下是从以下链接中获取的一些文字: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/99ae9d9c-1dd4-4acd-8d5d-6eb739adeb98

” 您好,

有可能。 这是创建段落/部分/表的超链接的一个小例子。

为了导航到网站,我们可以创建一个用于导航的帧控制。此示例中元素的层次关系如下:

框 - >的FlowDocument - >表 - >部分 - >第 - >超链接

在背后的代码中:

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);
        }

如果您对此有任何其他疑问,请随时提出。

感谢。 “

答案 1 :(得分:1)

如果您没有要求它是一个完整的FlowDocument,那么您可以使用普通的旧WPF TextBlock,并将超链接放入其中。

<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>

如果你需要滚动,只需在它周围放一个ScrollViewer。

如果你需要分页的多列查看器,那么你需要使用全部的FlowDocument,但如果你想要的只是带有超链接的文本,那么TextBlock + Hyperlink应该就是你所需要的。