WPF 中的文本控件(例如 RichTextControl)内的可点击链接?

发布于 2024-10-04 21:48:45 字数 171 浏览 4 评论 0原文

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

就像维基百科那样说。

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

我尝试了各种方法,但我无法做到这一点,尤其是单击功能不像网页那样,即单击一下即可打开工具中包含的网址。

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 the tools.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤慢歌 2024-10-11 21:48:45

如果您不要求它是一个完整的 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 应该就是您所需要的。

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.

め可乐爱微笑 2024-10-11 21:48:45

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

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


你好,

这是有可能的。
这是创建指向段落/部分/表格的超链接的小示例。

为了导航到网站,我们可以创建一个用于导航的框架控件。本例中元素的层次关系是这样的:

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

在后面的代码中:

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

如果您还有关于这个,请随意询问。

谢谢。 ”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文