富文本控件
<pre><code class="language-csharp">&lt;StackPanel&gt;
&lt;RichTextBox x:Name=&quot;_richTextBox&quot;&gt;
&lt;FlowDocument&gt;
&lt;Paragraph&gt;
123456
&lt;Bold Foreground=&quot;Red&quot;&gt;789&lt;/Bold&gt;
&lt;/Paragraph&gt;
&lt;Paragraph Foreground=&quot;DarkMagenta&quot;&gt;
123456789
&lt;/Paragraph&gt;
&lt;List MarkerStyle=&quot;Decimal&quot;&gt;
&lt;ListItem&gt;
&lt;Paragraph&gt;123&lt;/Paragraph&gt;
&lt;/ListItem&gt;
&lt;ListItem&gt;
&lt;Paragraph&gt;456&lt;/Paragraph&gt;
&lt;/ListItem&gt;
&lt;ListItem&gt;
&lt;Paragraph&gt;789&lt;/Paragraph&gt;
&lt;/ListItem&gt;
&lt;ListItem&gt;
&lt;Paragraph&gt;111213&lt;/Paragraph&gt;
&lt;/ListItem&gt;
&lt;/List&gt;
&lt;BlockUIContainer&gt;
&lt;Image Height=&quot;40&quot; Width=&quot;40&quot; Source=&quot;/image/question.png&quot; /&gt;
&lt;/BlockUIContainer&gt;
&lt;Table CellSpacing=&quot;5&quot;&gt;
&lt;Table.Columns&gt;
&lt;TableColumn&gt;&lt;/TableColumn&gt;
&lt;TableColumn&gt;&lt;/TableColumn&gt;
&lt;TableColumn&gt;&lt;/TableColumn&gt;
&lt;/Table.Columns&gt;
&lt;TableRowGroup&gt;
&lt;TableRow&gt;
&lt;TableCell&gt;
&lt;Paragraph&gt;姓名&lt;/Paragraph&gt;
&lt;/TableCell&gt;
&lt;TableCell&gt;
&lt;Paragraph&gt;性别&lt;/Paragraph&gt;
&lt;/TableCell&gt;
&lt;TableCell&gt;
&lt;Paragraph&gt;类型&lt;/Paragraph&gt;
&lt;/TableCell&gt;
&lt;/TableRow&gt;
&lt;TableRow&gt;
&lt;TableCell&gt;
&lt;Paragraph&gt;张三&lt;/Paragraph&gt;
&lt;/TableCell&gt;
&lt;TableCell&gt;
&lt;Paragraph&gt;男&lt;/Paragraph&gt;
&lt;/TableCell&gt;
&lt;TableCell&gt;
&lt;Paragraph&gt;管理&lt;/Paragraph&gt;
&lt;/TableCell&gt;
&lt;/TableRow&gt;
&lt;/TableRowGroup&gt;
&lt;/Table&gt;
&lt;/FlowDocument&gt;
&lt;/RichTextBox&gt;
&lt;Button Content=&quot;确定&quot; Name=&quot;_button&quot; Margin=&quot;10 5&quot; Padding=&quot;10&quot; Click=&quot;_button_Click&quot;&gt;&lt;/Button&gt;
&lt;/StackPanel&gt;
/// &lt;summary&gt;
/// RechText.xaml 的交互逻辑
/// &lt;/summary&gt;
public partial class RechText : Window
{
public RechText()
{
InitializeComponent();
//动态加载
//FlowDocument document = new FlowDocument();
//_richTextBox.Document = document;
//Paragraph paragraph = new Paragraph();
//paragraph.Foreground = Brushes.Red;
//paragraph.Inlines.Add(&quot;段落1&quot;);
//document.Blocks.Add(paragraph);
}
private void _button_Click(object sender, RoutedEventArgs e)
{
//读取文件
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = &quot;Text files (*.txt)|*.txt|All Files (*.*)|*.*&quot;;
if (openFile.ShowDialog() == true)
{
TextRange textRange = new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
using var stream = new FileStream(openFile.FileName,FileMode.Open,FileAccess.Read);
textRange.Load(stream, DataFormats.Text);
}
//获取富文本框中的内容
//TextRange textRange=new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
//MessageBox.Show(textRange.Text);
}
}</code></pre>