简体   繁体   中英

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

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

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) . 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 . Users will now be able to follow links with a 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. This class has a RequestNavigate event, which does what you need. 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.

You could do this in your code in the OnLoaded for your window:

    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:

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

As far as displaying images goes, this simply works out of the box with 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. 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. However, copying and pasting from Word to an RTB uses RTF as its interchange format ; maybe there's some problem with your HTML converter?

As an aside, it isn't easy to understand exactly what objects live inside a given 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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