WPF RichTextBox的常用方法和属性(内容的读取/导入等)
来源:互联网 发布:高中数学公式大全软件 编辑:程序博客网 时间:2024/06/10 21:26
1. 取得已被选中的内容:
(1)使用 RichTextBox.Document.Selection属性
(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text
2. 在XAML中增加内容给RichTextBox:
<RichTextBox IsSpellCheckEnabled="True">
<FlowDocument>
<Paragraph>
<!-- 这里加上你的内容 -->
This is a richTextBox. I can <Bold>Bold</Bold>, <Italic>Italicize</Italic>, <Hyperlink>Hyperlink stuff</Hyperlink> right in my document.
</Paragraph>
</FlowDocument>
</RichTextBox>
3. 缩短段间距,类似<BR>,而不是<P>
方法是使用Style定义段间距:
<RichTextBox>
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
<FlowDocument>
<Paragraph>
This is my first paragraph... see how there is...
</Paragraph>
<Paragraph>
a no space anymore between it and the second paragraph?
</Paragraph>
</FlowDocument>
</RichTextBox>
4. 从文件中读出纯文本文件后放进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);
}
5. 取得指定RichTextBox的内容:
private string GetText(RichTextBox richTextBox)
{
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
return textRange.Text;
}
6. 将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);
}
}
}
7. 将文件中的内容加载为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);
}
}
8. 将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);
}
}
9. 做个简单的编辑器:
<!-- Window1.xaml -->
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Open File" Click="OnOpenFile"/>
<MenuItem Header="_Save" Click="OnSaveFile"/>
<Separator/>
<MenuItem Header="E_xit" Click="OnExit"/>
</MenuItem>
</Menu>
<RichTextBox Name="richTextBox1"></RichTextBox>
</DockPanel>
// Window1.xaml.cs
private void OnExit(object sender, EventArgs e) {
this.Close();
}
private void OnOpenFile(object sender, EventArgs e) {
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true) {
LoadFile(ofd.SafeFileName, richTextBox1);
}
}
private void OnSaveFile(object sender, EventArgs e) {
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
if (sfd.ShowDialog() == true) {
SaveFile(sfd.SafeFileName, richTextBox1);
}
}
- WPF RichTextBox的常用方法和属性(内容的读取/导入等)
- WPF RichTextBox读取存储文本的方法和常用属性
- WPF 读取和存储RichTextBox的文档内容
- WPF SL 获取RichTextBox 的内容(string)
- WPF RICHTEXTBOX 和数据库读写图文的方法。
- WPF RICHTEXTBOX 和数据库读写图文的方法
- WPF richtextbox的使用
- WPF 处理RichTextBox, ListView等的Scroll Bar 事件(event)
- WPF 保存和加载 RichTextBox 内容
- WPF RichTextBox和rtf 复制等操作
- WPF读取和显示word的方法
- richtextbox的格式问题(居中等)
- WPF中RichTextBox实现和Winform一样的效果
- 自己封装的类用于操作RichTextBox内容,方便获取和设置RichTextBox内容
- .Net (C#) 在学习中遇到的RichTextBox控件的一些属性和方法
- WPF中RichTextBox的使用小窍门(翻译、整理)
- C#WPF里面RichTextBox的内容带格式存到数据库中
- 关于RichTextbox的一些属性
- 深度解析Objective-C内存管理教程
- android 集成第三方静态库的编译方法
- 换行符在Linux和Windows下的区别
- HID 设备PC端软件的开发
- 《iOS网络编程与云端应用最佳实践》上线了-源码下载,样章-正式发售了
- WPF RichTextBox的常用方法和属性(内容的读取/导入等)
- Cmake 基础
- Java编码问题汇总
- 第十三周任务2 多重继承
- Android App离线下载
- JNI开发:Class android.content.Context could not be found
- 一段被掩盖的的事实,一段被扭曲的历史
- 程序员在面试中如何占据主动?
- 比较BSTR大小