繁体   English   中英

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

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

由于在Stack Overflow中已经发布了一个问题,我试图将隐藏的Web浏览器的内容复制到RichTextBox中,这是我的代码

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

这是我的代码背后:

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

        }

这里的问题是,当HTML页面显示在RichTextBox控件中时,我无法单击链接以移动到另一页面,也无法显示图像……有什么建议吗?

此操作的主要目的是启用对html内容的选择,因为在Web浏览器中,我无法启用它并捕获所选文本的开始和结束位置,而这对于在HTML文本上突出显示是必需的(来自Epub) File)[如果我使用textrange ,并且用户选择一个单词并将其突出显示,则该单词在此html文件中重复存在时将被突出显示N次,但我只想突出显示所选的部分)。 是否有其他替代方法?

若要启用用户与RichTextBox超链接的交互,需要将RichTextBox.IsDocumentEnabled属性设置为true 用户现在可以通过Ctrl + Click链接:

<RichTextBox IsDocumentEnabled="True" />

如果将控件标记为只读,则只需单击一下,它们就可以跟随:

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

完成此操作后,仍然有必要在用户单击超链接时提供回调。 RichTextBox中的Hyperlink包含在Hyperlink类的实例中。 此类有一个RequestNavigate事件,它RequestNavigate您的需求。 您可以在每个Hyperlink上分别设置每个事件,但是将其设置在包含的RichTextBox上要容易得多,并允许路由事件机制将RequestNavigate到文本框。

您可以在窗口的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;
    }

或者,您可以通过为Hyperlink对象使用默认样式并提供回调,在富文本框的XAML中执行此操作:

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

(注-在Windows 7中测试)。

就显示图像而言,这可以通过RichTextBox 如果创建包含某些图像的Microsoft Word文档,然后将整个文档的内容复制并粘贴到RTB中,则会显示其中包含的图像。 如果我通过右键单击图像并选择“复制图像”,使用Firefox复制单个图像,然后将其粘贴到RTB中,则会显示出来。 但是,从Word复制和粘贴到RTB时将RTF用作其交换格式 也许您的HTML转换器有问题?

FlowDocument一句,要确切地了解给定FlowDocument哪些对象并不容易。 内部的结构没有得到很好的记录(有关概述,请参见此处 )。 我发现以下调试实用程序非常有用,它们将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