【问题标题】:Set a formatted string to a RichTextBox in WPF在 WPF 中将格式化字符串设置为 RichTextBox
【发布时间】:2014-04-14 19:38:39
【问题描述】:

如何将格式化字符串设置为 RichTextBox 控件,使其显示格式化文本,而不是纯文本。

字符串示例:

{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1046{\fonttbl{\f0\fnil\fcharset0   Calibri;}}
{\colortbl ;\red255\green0\blue0;}
{\*\generator Riched20 6.3.9600}\viewkind4\uc1 
\pard\sa200\sl276\slmult1\tx568\f0\fs22\lang22 This is an \cf1\ul example\cf0\ulnone\par
}

【问题讨论】:

    标签: wpf richtextbox


    【解决方案1】:

    您可以使用RichTextBox.Selection.Load 方法和Rtf 格式设置文本:

    var text = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1046{\fonttbl{\f0\fnil\fcharset0   Calibri;}}{\colortbl ;\red255\green0\blue0;}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\sa200\sl276\slmult1\tx568\f0\fs22\lang22 This is an \cf1\ul example\cf0\ulnone\par}";
    
    using (MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text)))
    {
        rtb.Selection.Load(stream, DataFormats.Rtf);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 2011-05-14
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      相关资源
      最近更新 更多