C# 将 RTF 文本设置为 WPF RichTextBox 控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1367256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:43:24  来源:igfitidea点击:

Set RTF text into WPF RichTextBox control

c#.netwpfrichtextbox

提问by Andrija

I have this RTF text:

我有这个 RTF 文本:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue0;\red255\green0\blue0;}
\viewkind4\uc1\pard\qc\cf1\fs16 test \b bold \cf2\b0\i italic\cf0\i0\fs17 
\par }

How to set this text into WPF RichTextBox?

如何将此文本设置为WPF RichTextBox



Solution:

解决方案:

public void SetRTFText(string text)
{
    MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text));
    this.mainRTB.Selection.Load(stream, DataFormats.Rtf);
}

Thanks for help from Henk Holterman.

感谢 Henk Holterman 的帮助。

采纳答案by Henk Holterman

Do you really have to start with a string?

你真的必须从一个字符串开始吗?

One method to load RTF is this:

加载 RTF 的一种方法是:

rtfBox.Selection.Load(myStream, DataFormats.Rtf);

You probably should call SelectAll() before that if you want to replace existing text.

如果您想替换现有文本,您可能应该在此之前调用 SelectAll()。

So, worst case, you'll have to write your string to a MemoryStream and then feed that stream to the Load() method. Don't forget to Position=0 in between.

因此,最坏的情况是,您必须将字符串写入 MemoryStream,然后将该流提供给 Load() 方法。不要忘记在两者之间设置 Position=0。

But I'm waiting to see somebody to come up with something more elegant.

但我在等着看有人想出更优雅的东西。

回答by J. Random Coder

Edit: This answer assumes WinForms instead of WPF.

编辑:此答案假定 WinForms 而不是 WPF。

Simply use RichTextBox.Rtf:

只需使用 RichTextBox.Rtf:

string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}} {\colortbl ;\red0\green0\blue0;\red255\green0\blue0;} \viewkind4\uc1\pard\qc\cf1\fs16 test \b bold \cf2\b0\i italic\cf0\i0\fs17  \par } ";
richTextBox1.Rtf = rtf;

回答by Rhyous

I wrote a really slick solution by extending the RichTextBox class to allow Binding to an actual rich text file.

我通过扩展 RichTextBox 类来允许绑定到实际的富文本文件,从而编写了一个非常巧妙的解决方案。

I came across this question/answer but didn't really get what I was looking for, so hopefully what I have learned will help out those who read this in the future.

我遇到了这个问题/答案,但并没有真正得到我想要的东西,所以希望我学到的东西能帮助那些将来读到这个的人。

Loading a RichTextBox from an RTF file using binding or a RichTextFile control

使用绑定或 RichTextFile 控件从 RTF 文件加载 RichTextBox

回答by P.Brian.Mackey

Create an Extension method

创建扩展方法

    public static void SetRtf(this RichTextBox rtb, string document)
    {
        var documentBytes = Encoding.UTF8.GetBytes(document);
        using (var reader = new MemoryStream(documentBytes))
        {
            reader.Position = 0;
            rtb.SelectAll();
            rtb.Selection.Load(reader, DataFormats.Rtf);
        }
    }

Then you can do WinForm-esque style

然后你可以做WinForm-esque风格

richTextBox1.SetRtf(rtf);

richTextBox1.SetRtf(rtf);

回答by user10026242

string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}} {\colortbl ;\red0\green0\blue0;\red255\green0\blue0;} \viewkind4\uc1\pard\qc\cf1\fs16 test \b bold \cf2\b0\i italic\cf0\i0\fs17  \par } ";
richTextBox1.Rtf = rtf;

It's working fine

它工作正常