简体   繁体   English

如何在RichTextBox(WPF)中启用图像和链接?

[英]How to Enable Images and links in RichTextBox (WPF)?

I have tried to copy content of a hidden webbrowser to RichTextBox thanks to an already posted question in Stack Overflow , this is my code 由于在Stack Overflow中已经发布了一个问题,我试图将隐藏的Web浏览器的内容复制到RichTextBox中,这是我的代码

<WebBrowser Name="webBrowser1" helper:WebBrowserHelper.BindableSource="C:\Users\med\Desktop\cover.xhtml" Visibility="Hidden"/>
        <RichTextBox IsReadOnly="True" Name="richTextBox1" />

here is my code behind : 这是我的代码背后:

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            dynamic document = webBrowser1.Document;
            document.ExecCommand("SelectAll", false, null);
            document.ExecCommand("Copy", false, null);
            richTextBox1.Paste();

        }

the problem here is that when the Html page is displayed in my RichTextBox control , i cannot click on links to move to another page , also Images are not displayed ... Any suggestion ? 这里的问题是,当HTML页面显示在RichTextBox控件中时,我无法单击链接以移动到另一页面,也无法显示图像……有什么建议吗?

The main objectif of this manipulation is to enable selection on my html content , because in web browser , i can't enable it and catch the start and end position of the selected text which is necessary to make highlights on Html Text (from the Epub File) [If i use the textrange and the user select a word and highlight it , this word will be highlight N times if it exists in duplication in this html file , but i want to highlight only the selected Portion) . 此操作的主要目的是启用对html内容的选择,因为在Web浏览器中,我无法启用它并捕获所选文本的开始和结束位置,而这对于在HTML文本上突出显示是必需的(来自Epub) File)[如果我使用textrange ,并且用户选择一个单词并将其突出显示,则该单词在此html文件中重复存在时将被突出显示N次,但我只想突出显示所选的部分)。 Is there any possible alternative for this ? 是否有其他替代方法?

To enable user interaction with hyperlinks in a RichTextBox , you need to set the RichTextBox.IsDocumentEnabled property to true . 若要启用用户与RichTextBox超链接的交互,需要将RichTextBox.IsDocumentEnabled属性设置为true Users will now be able to follow links with a Ctrl+Click: 用户现在可以通过Ctrl + Click链接:

<RichTextBox IsDocumentEnabled="True" />

If you mark the control as read only, they will be able to follow with just a Click: 如果将控件标记为只读,则只需单击一下,它们就可以跟随:

<RichTextBox IsDocumentEnabled="True" IsReadOnly="True" />

Having done that, it's still necessary to actually provide a callback when the user clicks the hyperlink. 完成此操作后,仍然有必要在用户单击超链接时提供回调。 Hyperlinks in a RichTextBox are contained in instances of the Hyperlink class. RichTextBox中的Hyperlink包含在Hyperlink类的实例中。 This class has a RequestNavigate event, which does what you need. 此类有一个RequestNavigate事件,它RequestNavigate您的需求。 You could set each event individually on each Hyperlink , but it's much, much easier to set it on the encompassing RichTextBox , and allow the routed event mechanism to bubble the RequestNavigate up to the text box. 您可以在每个Hyperlink上分别设置每个事件,但是将其设置在包含的RichTextBox上要容易得多,并允许路由事件机制将RequestNavigate到文本框。

You could do this in your code in the OnLoaded for your window: 您可以在窗口的OnLoaded中的代码中执行此操作:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        richTextBox1.AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(HyperLink_RequestNavigate));
    }

    void HyperLink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
    {
        Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
        e.Handled = true;
    }

Alternatively, you could do this in the XAML for your rich text box by using a default style for Hyperlink objects and providing the callback: 或者,您可以通过为Hyperlink对象使用默认样式并提供回调,在富文本框的XAML中执行此操作:

        <RichTextBox Name="richTextBox1" AcceptsTab="True" IsDocumentEnabled="True">
            <RichTextBox.Resources>
                <Style TargetType="Hyperlink">
                    <EventSetter Event="RequestNavigate" Handler="HyperLink_RequestNavigate" />
                </Style>
            </RichTextBox.Resources>
        </RichTextBox>

(Note - tested in Windows 7). (注-在Windows 7中测试)。

As far as displaying images goes, this simply works out of the box with RichTextBox . 就显示图像而言,这可以通过RichTextBox If I create a Microsoft Word document containing some images, then copy and paste the entire document's content into an RTB, the included images show up. 如果创建包含某些图像的Microsoft Word文档,然后将整个文档的内容复制并粘贴到RTB中,则会显示其中包含的图像。 If I copy a single image with Firefox by right-clicking on the image and choosing "Copy Image", then paste it into an RTB, it shows up. 如果我通过右键单击图像并选择“复制图像”,使用Firefox复制单个图像,然后将其粘贴到RTB中,则会显示出来。 However, copying and pasting from Word to an RTB uses RTF as its interchange format ; 但是,从Word复制和粘贴到RTB时将RTF用作其交换格式 maybe there's some problem with your HTML converter? 也许您的HTML转换器有问题?

As an aside, it isn't easy to understand exactly what objects live inside a given FlowDocument . FlowDocument一句,要确切地了解给定FlowDocument哪些对象并不容易。 The structure of what might be inside isn't well documented (see here for an overview). 内部的结构没有得到很好的记录(有关概述,请参见此处 )。 I have found the following debug utilities useful, they convert the FlowDocument to XAML and output it in a readable format: 我发现以下调试实用程序非常有用,它们将FlowDocument转换为XAML并以可读格式输出:

public static class FlowDocumentHelper
{
    public static string ToFormattedXamlString(this FlowDocument doc)
    {
        if (doc == null)
            return null;
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = "    ";
        var sb = new StringBuilder();
        var xmlWriter = XmlWriter.Create(sb, settings);
        XamlWriter.Save(doc, xmlWriter);
        return sb.ToString();
    }

    public static string DebugFlowDocumentXaml(this FlowDocument doc)
    {
        var str = doc.ToFormattedXamlString();
        Debug.WriteLine(str);
        return str;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM