日期:2014-05-20  浏览次数:20866 次

WPF richTextBox问题
方法一
//保存
MemoryStream s1 = new MemoryStream();
TextRange documentTextRange1 = new TextRange(this.richTextBox1.Document.ContentStart, this.richTextBox1.Document.ContentEnd);
documentTextRange1.Save(s1, DataFormats.XamlPackage);
string value = Convert.ToBase64String(s1.ToArray());//保存的数据是ToBase64String是我想要来保存到库的类型

//获取
MemoryStream set1 = new MemoryStream((Convert.FromBase64String(value)));
TextRange TR1 = new TextRange(this.richTextBox2.Document.ContentStart, this.richTextBox2.Document.ContentEnd);
TR1.Load(set1, DataFormats.XamlPackage);

//显示
richTextBox2.AppendText("用户 " + DateTime.Now.ToLongTimeString() + "\r\n");
richTextBox2.AppendText(TR1.Text + "\r\n");
   
//显示结果
输入123显示:

123用户 14:28:21
123用户 14:28:21

疑问:怎么123是在用户前面呢,而且还打印2次,还帮我清空richTextBox


方法2
//保存
FlowDocument document = richTextBox1.Document;
System.IO.Stream s = new System.IO.MemoryStream();
System.Windows.Markup.XamlWriter.Save(document, s);
byte[] data2 = new byte[s.Length];
s.Position = 0;
s.Read(data2, 0, data2.Length);
s.Close();
//获取
System.IO.Stream ss = new System.IO.MemoryStream(data2);
FlowDocument docs = System.Windows.Markup.XamlReader.Load(ss) as FlowDocument;
ss.Close();
//显示
richTextBox2.AppendText("用户 " + DateTime.Now.ToLongTimeString()+ "\r\n");
richTextBox2.AppendText(docs.ToString()+ "\r\n");

显示结果:输入123
用户 14:28:22
System.Windows.Documents.FlowDocument
疑问:如果让docs.ToString()转为我要显示的值

分不多,麻烦高手指导下

------解决方案--------------------
http://www.cnblogs.com/isGood/archive/2011/09/06/2168807.html