WPF RichTextBox控件用法

WPF RichTextBox控件用法


1. 取得已被选中的内容:

(1)使用RichTextBox.Document.Selection属性

(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text

2.WPF RictTextBox内容清空方式

     txtXml.Document.Blocks.Clear();

3. 从文件中读出纯文本文件后放进RichTextBox或直接将文本放进RichTextBox中:

private void LoadTextFile(RichTextBox richTextBox, string filename)
{
    richTextBox.Document.Blocks.Clear();
    using (StreamReader streamReader = File.OpenText(filename)) {
           Paragraph paragraph = new Paragraph();
           paragraph.Text = streamReader.ReadToEnd();
           richTextBox.Document.Blocks.Add(paragraph);
    }
}

private void LoadText(RichTextBox richTextBox, string txtContent)
{
    richTextBox.Document.Blocks.Clear();
    Paragraph paragraph = new Paragraph();
    paragraph.Text = txtContent;
    richTextBox.Document.Blocks.Add(paragraph);
}

  1. 取得指定RichTextBox的内容:
private string GetText(RichTextBox richTextBox) 
{
        TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
        return textRange.Text;
}
  1. 将RTF (rich text format)放到RichTextBox中:
  private static void LoadRTF(string rtf, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(rtf)) {
                throw new ArgumentNullException();
            }
            TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            using (MemoryStream rtfMemoryStream = new MemoryStream()) {
                using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream)) {
                    rtfStreamWriter.Write(rtf);
                    rtfStreamWriter.Flush();
                    rtfMemoryStream.Seek(0, SeekOrigin.Begin);
                    //Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
                }
            }
        }
  1. 将文件中的内容加载为RichTextBox的内容
 private static void LoadFile(string filename, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(filename)) {
                throw new ArgumentNullException();
            }
            if (!File.Exists(filename)) {
                throw new FileNotFoundException();
            }
            using (FileStream stream = File.OpenRead(filename)) {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                string ext = System.IO.Path.GetExtension(filename);
                if (String.Compare(ext, ".xaml",true) == 0) {
                    dataFormat = DataFormats.Xaml;
                }
                else if (String.Compare(ext, ".rtf", true) == 0) {
                    dataFormat = DataFormats.Rtf;
                }
                documentTextRange.Load(stream, dataFormat);
            }        
        }
  1. 将RichTextBox的内容保存为文件:
 private static void SaveFile(string filename, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(filename)) {
                throw new ArgumentNullException();
            }
            using (FileStream stream = File.OpenWrite(filename)) {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                string ext = System.IO.Path.GetExtension(filename);
                if (String.Compare(ext, ".xaml", true) == 0) {
                    dataFormat = DataFormats.Xaml;
                }
                else if (String.Compare(ext, ".rtf", true) == 0) {
                    dataFormat = DataFormats.Rtf;
                }
                documentTextRange.Save(stream, dataFormat);
            }
        }

8.读取与写入图片和文本操作

//读取RichTextBox的内容到string

string GetTextByRichBox(RichTextBox box)
{
MemoryStream s = new MemoryStream();
TextRange documentTextRange = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);
documentTextRange.Save(s, DataFormats.XamlPackage);
return Convert.ToBase64String(s.ToArray());
}

9.将string的内容转换成图片显示在RichTextBox中

private ShowTextToRichBox(RichTextBox box)
{
MemoryStream s = new MemoryStream((Convert.FromBase64String(Convert.ToString(dr["D_DESC"]))));
TextRange TR = new TextRange(box.Document.ContentStart,box.Document.ContentEnd);
TR.Load(s, DataFormats.XamlPackage);
}
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值