替换 RichTextBox 文本但保留格式

发布于 2024-10-12 14:38:21 字数 1042 浏览 3 评论 0原文

任何人都可以帮我解释一下这个问题吗?我有一个 RichTextBox,我正在将 xaml 文件加载到其中。我需要用真实数据替换 RichTxtBox 文本的某些部分,即“[our_name]”替换为“Billie Brags”。我的 xaml 文件包含粗体和粗体等格式。字体大小。

当我运行代码(如下所示)时,我可以更改文本,但我会丢失格式。

知道我该如何做到这一点并保持格式吗?

谢谢

            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            using (fs)
            {
                TextRange RTBText = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
                RTBText.Load(fs, DataFormats.Xaml);
            }



        TextRange tr = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
        string rtbContent = tr.Text;
        rtbContent = rtbContent.Replace("<our_name>", "Billie Brags");
        System.Windows.MessageBox.Show(rtbContent);

        FlowDocument myFlowDoc = new FlowDocument();

        // Add paragraphs to the FlowDocument
        myFlowDoc.Blocks.Add(new Paragraph(new Run(rtbContent)));
        rtb_wording.Document = myFlowDoc;

Can anybody cast some light on this for me, I have a RichTextBox which im loading an xaml file into it. I need to Replace certain Parts of the RichTxtBox's text with real data i.e. '[our_name]' is replaced with 'Billie Brags'. My xaml file contains formatting like bold & font size.

When I run my code (shown below) I can change the text OK but im loosing the formatting.

Any idea how I can do this and keep the formatting?

Thank you

            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            using (fs)
            {
                TextRange RTBText = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
                RTBText.Load(fs, DataFormats.Xaml);
            }



        TextRange tr = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
        string rtbContent = tr.Text;
        rtbContent = rtbContent.Replace("<our_name>", "Billie Brags");
        System.Windows.MessageBox.Show(rtbContent);

        FlowDocument myFlowDoc = new FlowDocument();

        // Add paragraphs to the FlowDocument
        myFlowDoc.Blocks.Add(new Paragraph(new Run(rtbContent)));
        rtb_wording.Document = myFlowDoc;

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

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

发布评论

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

评论(2

樱花细雨 2024-10-19 14:38:22

它的工作原理,这就是我最终做到的,不是太漂亮,但它的功能。 WPF RTB 确实应该像 winforms 一样具有 rtf 属性...

感谢 Kent 让我走上了正确的道路。

            var textRange = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
            string rtf;
            using (var memoryStream = new MemoryStream())
            {
                textRange.Save(memoryStream, DataFormats.Rtf);
                rtf = ASCIIEncoding.Default.GetString(memoryStream.ToArray());
            }

            rtf = rtf.Replace("<our_name>", "Bob Cratchet");

            MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(rtf));
            rtb_wording.SelectAll();
            rtb_wording.Selection.Load(stream, DataFormats.Rtf);

Its working, this is how I did it in the end, not too pretty but it functions. WPF RTB really should have rtf property like winforms...

Thanks to Kent for putting me on the right track.

            var textRange = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
            string rtf;
            using (var memoryStream = new MemoryStream())
            {
                textRange.Save(memoryStream, DataFormats.Rtf);
                rtf = ASCIIEncoding.Default.GetString(memoryStream.ToArray());
            }

            rtf = rtf.Replace("<our_name>", "Bob Cratchet");

            MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(rtf));
            rtb_wording.SelectAll();
            rtb_wording.Selection.Load(stream, DataFormats.Rtf);
﹎☆浅夏丿初晴 2024-10-19 14:38:22

我相信您需要以 RTF 格式保存 TextRange 的内容,然后重新加载 RTB 的内容。我还没有尝试过,所以不确定它是否有效(目前在 Linux 上,所以无法测试):

var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
string rtf;

using (var memoryStream = new MemoryStream())
using (var streamReader = new StreamReader(memoryStream))
{
    textRange.Save(memoryStream, DataFormats.Rtf);
    rtf = streamReader.ReadToEnd();
}

rtf = rtf.Replace("<whatever>", "whatever else");

using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(rtf)))
{
    textRange.Load(memoryStream, DataFormats.Rtf);
}

I believe you'll need to save the contents of the TextRange in RTF format, and then reload the contents of the RTB. I haven't tried this so not sure it will work (on linux at the moment so can't test):

var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
string rtf;

using (var memoryStream = new MemoryStream())
using (var streamReader = new StreamReader(memoryStream))
{
    textRange.Save(memoryStream, DataFormats.Rtf);
    rtf = streamReader.ReadToEnd();
}

rtf = rtf.Replace("<whatever>", "whatever else");

using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(rtf)))
{
    textRange.Load(memoryStream, DataFormats.Rtf);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文