在 WPF FlowDocument 中的指定位置插入超链接

发布于 2024-09-15 02:03:27 字数 957 浏览 2 评论 0原文

我想以编程方式将 WPF 超链接元素插入 FlowDocument 中。

目标是创建一个工具栏按钮,该按钮将获取 RichTextBox 中的一系列文本并将其替换为超链接。它与您在网络上看到的用于在 wiki 或博客(或 StackOverflow)上创建超链接的界面相同。

我可以像这样找到所选文本的 TextRange:

    TextRange tr = new TextRange(
    MyRichTextBox.Selection.Start,
    MyRichTextBox.Selection.End);

我正在尝试将超链接 Xaml 填充到 TextRange 中,如下所示:

    string rawXaml = "<Hyperlink xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" NavigateUri=\"http://www.google.com/\">Google Home Page</Hyperlink>";

    using(MemoryStream stream = new MemoryStream())
    {
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(rawXaml);
        writer.Flush();
        stream.Position = 0;

        if (tr.CanLoad(DataFormats.Xaml))
        {
            tr.Load(stream, DataFormats.Xaml);
        } 
    }

但我似乎仍然将纯文本粘贴到 RichTextBox 中。

我在这里做错了什么?有更好的方法来完成我想做的事情吗?

I'd like to insert a WPF Hyperlink element into a FlowDocument programmatically.

The objective is to create a toolbar button that would take a run of text within a RichTextBox and replace it with a Hyperlink. It's the same sort of interface you see on the web for creating hyperlinks on wikis or in blogs (or on StackOverflow).

I can find the TextRange of the selected text like this:

    TextRange tr = new TextRange(
    MyRichTextBox.Selection.Start,
    MyRichTextBox.Selection.End);

And I'm attempting to stuff the Hyperlink Xaml into the TextRange like so:

    string rawXaml = "<Hyperlink xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" NavigateUri=\"http://www.google.com/\">Google Home Page</Hyperlink>";

    using(MemoryStream stream = new MemoryStream())
    {
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(rawXaml);
        writer.Flush();
        stream.Position = 0;

        if (tr.CanLoad(DataFormats.Xaml))
        {
            tr.Load(stream, DataFormats.Xaml);
        } 
    }

But I still seem to get plain text pasted into the RichTextBox.

What am I doing wrong here? Are there better ways to accomplish what I'm trying to do?

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

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

发布评论

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

评论(1

帅气称霸 2024-09-22 02:03:27

使用接受 TextPointer 的超链接的构造函数:

tr.Text = "";
Run run = new Run("Google Home Page");
Hyperlink hlink = new Hyperlink(run, tr.Start);
hlink.NavigateUri = new Uri("http://www.google.com/");

或者,首先更改文本,然后使用接受两个 TextPointer 的超链接:

tr.Text = "Google Home Page";
Hyperlink hlink = new Hyperlink(tr.Start, tr.End);
hlink.NavigateUri = new Uri("http://www.google.com/");

编辑:如果您想使用 TextRange.Load,请尝试将超链接包装在 Span 中:

string rawXaml = "<Span xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Hyperlink NavigateUri=\"http://www.google.com/\">Google Home Page</Hyperlink></Span>";

我不是确定为什么当普通超链接不起作用时,它会起作用,但它更接近 TextRange.Save 返回的内容。

Use the constructor for Hyperlink that takes in a TextPointer:

tr.Text = "";
Run run = new Run("Google Home Page");
Hyperlink hlink = new Hyperlink(run, tr.Start);
hlink.NavigateUri = new Uri("http://www.google.com/");

Or, change the text first and then use the one that takes two TextPointers:

tr.Text = "Google Home Page";
Hyperlink hlink = new Hyperlink(tr.Start, tr.End);
hlink.NavigateUri = new Uri("http://www.google.com/");

Edit: If you want to use TextRange.Load, try wrapping the Hyperlink in a Span:

string rawXaml = "<Span xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Hyperlink NavigateUri=\"http://www.google.com/\">Google Home Page</Hyperlink></Span>";

I'm not sure why this works when a plain Hyperlink doesn't, but it's closer to what gets returned by TextRange.Save.

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