保存RichTextBox的文本到数据库,以及如何对RichTextBox的Document做绑定
2012-02-01 15:21
381 查看
如何保存RichTextBox的文本到数据库,包括格式等等,然后需要的再从数据库取出来,并且显示到RichTextBox中。
其实,RichTextBox的文本是一个FlowDocument类型的对象,我们只需要利用XamlReader和XamlWriter就能很好的完成上述工作。
【保存Document到流】
FlowDocument document = richTextBox.Document;
Stream s = new MemoryStream(); // 其他的什么Stream类型都没问题
XamlWriter.Save(document, s);
// 拿到s之后,再转化成二进制数据写到数据库就OK了
byte[] data = new byte[s.Length];
s.Position = 0;
s.Read(byte, 0, s.Length);
s.Close();
// 拿着data干啥都行
// ……
【从数据库中读取】
// data是从数据库中读出来的二进制数据
Stream s = new MemoryStream(data);
FlowDocument doc = XamlReader.Load(s) as FlowDocument;
s.Close();
richTextBox.Document = doc;
PS:有人问过我如何对RichTextBox的Document属性做绑定,由于RichTextBox的Document属性不是一个DependencyProperty,
所以我采用的是继承RichTextBox,自己定义一个BindableDocument的DependencyProperty来做。
public class BindableRichTextBox : RichTextBox
{
public FlowDocument BindableDocument
{
get { return (FlowDocument)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("BindableDocument", typeof(FlowDocument), typeof(BindableRichTextBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnTextPropertyChanged)));
private static void OnTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
BindableRichTextBox textBox = sender as BindableRichTextBox;
if (textBox != null)
{
textBox._changeFromBinding = true;
textBox.OnTextPropertyChanged(e);
}
}
// 防止死锁,比如A变了通知B,B变了又通知A
private bool _changeFromBinding = false;
// 当BindableDocument属性变化时,通知Document属性
protected virtual void OnTextPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (_changeFromBinding)
{
this.Document = e.NewValue as FlowDocument;
}
}
// 当Document属性变化时,通知BindableDocument属性
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if (!_changeFromBinding)
{
this.BindableDocument = this.Document;
}
// 放到外面
_changeFromBinding = false;
}
}
其实,RichTextBox的文本是一个FlowDocument类型的对象,我们只需要利用XamlReader和XamlWriter就能很好的完成上述工作。
【保存Document到流】
FlowDocument document = richTextBox.Document;
Stream s = new MemoryStream(); // 其他的什么Stream类型都没问题
XamlWriter.Save(document, s);
// 拿到s之后,再转化成二进制数据写到数据库就OK了
byte[] data = new byte[s.Length];
s.Position = 0;
s.Read(byte, 0, s.Length);
s.Close();
// 拿着data干啥都行
// ……
【从数据库中读取】
// data是从数据库中读出来的二进制数据
Stream s = new MemoryStream(data);
FlowDocument doc = XamlReader.Load(s) as FlowDocument;
s.Close();
richTextBox.Document = doc;
PS:有人问过我如何对RichTextBox的Document属性做绑定,由于RichTextBox的Document属性不是一个DependencyProperty,
所以我采用的是继承RichTextBox,自己定义一个BindableDocument的DependencyProperty来做。
public class BindableRichTextBox : RichTextBox
{
public FlowDocument BindableDocument
{
get { return (FlowDocument)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("BindableDocument", typeof(FlowDocument), typeof(BindableRichTextBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnTextPropertyChanged)));
private static void OnTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
BindableRichTextBox textBox = sender as BindableRichTextBox;
if (textBox != null)
{
textBox._changeFromBinding = true;
textBox.OnTextPropertyChanged(e);
}
}
// 防止死锁,比如A变了通知B,B变了又通知A
private bool _changeFromBinding = false;
// 当BindableDocument属性变化时,通知Document属性
protected virtual void OnTextPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (_changeFromBinding)
{
this.Document = e.NewValue as FlowDocument;
}
}
// 当Document属性变化时,通知BindableDocument属性
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if (!_changeFromBinding)
{
this.BindableDocument = this.Document;
}
// 放到外面
_changeFromBinding = false;
}
}
相关文章推荐
- 如何保存RichTextBox的文本到数据库?以及如何对RichTextBox的Document做绑定?
- 【WPF】如何保存RichTextBox的文本到数据库?以及如何对RichTextBox的Document做绑定? - Life Is Art - 博客园
- 【WPF】如何保存RichTextBox的文本到数据库?以及如何对RichTextBox的Document做绑定?
- 如何保存RichTextBox的文本到数据库from 缝缝补补又三年
- 在freeTextbox中显示Script脚本内容以及freeTextbox中单引号如何保存至数据库
- 如何在 jsp 上输出数据库中保存的文本
- WPF listview 如何绑定 Oracle 数据库 以及不错的oralceHelper.cs 文件
- 在Qt中如何加载图像以及保存图像到数据库中
- 如何将数据库二进制图片保存到硬盘上
- 如何将Jenkins multiline string parameter的多行文本优雅的保存为文件
- 如何将数据库连接字符串保存在配置文件中并读取
- 如何处理数据库中海量数据,以及处理数据库海量数据的经验和技巧
- 解读郭神LitePal源码-数据库的创建以及如何修改源码来创建外置数据库
- flask如何完成数据库内容在html页面上的显示以及相应数据的删除
- log4net 如何跨域或者跨进程日志记录在同一个日志文件中以及数据库中
- DEDECMS如何修改数据库密码以及忘记了后台密码怎么办
- 如何对dataset中进行修改并把修改后的数据保存到数据库中?
- 如何让网页面自动发送请求 - 以及 保持原有的文本格式
- 如何通過編程獲取列表項目的附件以及多行文本中的文件內容
- 如何禁止App在后台运行以及如何保存和恢复App的状态