WPF RichTextBox 使用示例
一、取出RichTextBox值
1、取得RichTextBox 文字
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
string text = DataFormats.Text;
2、取得RichTextBox内的XAML文档
string xaml = System.Windows.Markup.XamlWriter.Save(richTextBox.Document);
3、将richTextBox的类容以二进制数据的方法取出
FlowDocument document = richTextBox.Document;
System.IO.Stream s = new System.IO.MemoryStream();
System.Windows.Markup.XamlWriter.Save(document, s);
byte[] data = new byte[s.Length];
s.Position = 0;
s.Read(data, 0, data.Length);
s.Close();
二、给RichTextBox赋值
String字符串赋值给RichTextBox
FlowDocument doc = new FlowDocument();
Paragraph p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
Run r = new Run("文字"); // Run 是一个 Inline 的标签
p.Inlines.Add(r);
doc.Blocks.Add(p);
richTextBox.Document = doc;
以上可以简化为
richTextBox.Document = new FlowDocument(new Paragraph(new Run("文字")));
第一种方法:将XAML字符串转换为数据流赋值给richTextBox中
System.IO.StringReader sr = new System.IO.StringReader(xw);
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);
richTextBox1.Document = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);
第二种方法:将Text字符串转换为数据流赋值给richTextBox中
using (StreamReader streamReader = File.OpenText(filename)) {
Paragraph paragraph = new Paragraph();
paragraph.Text = streamReader.ReadToEnd();
richTextBox.Document.Blocks.Add(paragraph);
}
第三种方法:将二进制数据赋值给richTextBox
System.IO.Stream ss = new System.IO.MemoryStream(data);
FlowDocument doc = System.Windows.Markup.XamlReader.Load(ss) as FlowDocument;
ss.Close();
richTextBox1.Document = doc;
三、清空RichTextBox的方法
System.Windows.Documents.FlowDocument doc = richTextBox.Document;
doc.Blocks.Clear();
四、复制, 剪切,粘贴,全选
方法一:将键击发送到应用程序的方法
richTextBox.Focus();
SendKeys.Send("^a");//全选
SendKeys.Send("^c");//复制
SendKeys.Send("^x");//剪切
SendKeys.Send("^v");//粘贴
方法二:直接通过命令操作
<RichTextBox Name="richTextBox" Height="200" IsEnabled="True">
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="复制" Command="ApplicationCommands.Copy" />
<MenuItem Header="粘贴" Command="ApplicationCommands.Paste" />
<MenuItem Header="剪切" Command="ApplicationCommands.Cut" />
</ContextMenu>
</RichTextBox.ContextMenu>
</RichTextBox>
五、RichTextBox增加XAML内容
<RichTextBox Name="richTextBox" Height="200" IsEnabled="True">
<FlowDocument>
<Paragraph>
<Bold>Bold</Bold>
<Italic>Italicize</Italic>
<Hyperlink>Hyperlink stuff</Hyperlink>
</Paragraph>
</FlowDocument>
</RichTextBox>
六、 RichTextBox 设置Style内容样式,如:缩短段间距
<RichTextBox Name="richTextBox" Height="200" IsEnabled="True">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
七、RichTextBox插入图片,点击按钮选取图片
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "Text Files (*.jpg; *.png; *.gif)|*.jpg;*.png;*.gif";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
BitmapImage bImg = new BitmapImage();
bImg.BeginInit();
//bImg.UriSource = new Uri(@"C:\Users\admin\source\repos\WPFRichTextBox\WPFRichTextBox\ext_apk.png", UriKind.Relative);
bImg.UriSource = new Uri(ofd.FileName, UriKind.RelativeOrAbsolute);
bImg.EndInit();
img.Source = bImg;
//调整图片大小
if (bImg.Height > 100 || bImg.Width > 100)
{
img.Height = bImg.Height * 0.2;
img.Width = bImg.Width * 0.2;
}
//图片缩放模式
//img.Stretch = Stretch.Uniform;
new InlineUIContainer(img, richTextBox.Selection.Start); //插入图片到选定位置
}
八、查找段落 Paragraph
var doc = this.richTextBox.Document;
var blocks = doc.Blocks.ToArray();
var paragraphs = blocks.Where(b => b is Paragraph).ToArray();
Paragraph paragraph = (Paragraph)paragraphs[0];
//修改段落内容
p.Inlines.Clear();
p.Inlines.Add(new Run("天涯共此时"));
九、查找FlowDocument中的所有图像
1、查找Paragraph中的所有图像
List<System.Windows.Controls.Image> FindAllImagesInParagraph(Paragraph paragraph)
{
List<System.Windows.Controls.Image> result = null;
foreach (var inline in paragraph.Inlines)
{
var inlineUIContainer = inline as InlineUIContainer;
if (inlineUIContainer != null)
{
var image = inlineUIContainer.Child as System.Windows.Controls.Image;
if (image != null)
{
if (result == null)
result = new List<System.Windows.Controls.Image>();
result.Add(image);
}
}
}
return result;
}
十、RichTextBox 启用 WPF 控件
默认情况下,RichTextBox 禁用处理 Button Click,ContextMenu 等
1、实现步骤,重写 FlowDocument
public class TalkFlowDocument : FlowDocument { protected override bool IsEnabledCore { get { return true; } } }
2、XAML 引用
<Window xmlns:local="clr-namespace:HitoolCore"> <Grid> <RichTextBox IsReadOnly="True"> <local:TalkFlowDocument> <Paragraph> <TextBlock Text="你好"/> <Paragraph.ContextMenu> <ContextMenu> <MenuItem Header="复制" /> <MenuItem Header="引用" /> </ContextMenu> </Paragraph.ContextMenu> </Paragraph> <Paragraph> <TextBlock Text="以后请多关照"/> <Paragraph.ContextMenu> <ContextMenu> <MenuItem Header="撤回" /> <MenuItem Header="转发" /> </ContextMenu> </Paragraph.ContextMenu> </Paragraph> </local:TalkFlowDocument> </RichTextBox> </Grid> </Window>
参考:http://www.voidcn.com/article/p-vrncotwa-bxx.html
WPF RichTextBox的使用总结
https://www.cnblogs.com/gaobing/p/3865254.html
WPF中设置RichTextBox的行间距
https://www.cnblogs.com/wzwyc/p/17835749.html