1.RichTextBox 控件介绍

TextBox : TextBoxBase

FlowDocument

inline run hyperlink

block paragraph section blockuicontainer

完成功能丰富的编辑器,RichTextBox,配合相应的按钮来制作出来

2.具体案例

<Window x:Class="WpfAppTest.RichTextBoxWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppTest"
mc:Ignorable="d"
Title="RichTextBoxWindow" Height="450" Width="800">
<Grid>
<RichTextBox Name="rtbContent" HorizontalAlignment="Left" Height="229" Margin="79,78,0,0" VerticalAlignment="Top" Width="467" IsDocumentEnabled="True">
<FlowDocument>
<Paragraph TextIndent="24">
<Run Text="RichTextBox"/>
<Hyperlink Click="Hyperlink_Click">百度一下</Hyperlink>
</Paragraph>
<Paragraph>
<Run Text="这是另一段文本"/>
</Paragraph>
<Section >
<Paragraph>
<Run Text="这是另一段文本2"/>
<InlineUIContainer>
<TextBox Text="12.00"/>
</InlineUIContainer>
</Paragraph>
<Paragraph>
<Run Text="这是另一段文本3"/>
</Paragraph>
<BlockUIContainer >
<StackPanel Orientation="Horizontal">
<Label Content="admin" Width="60" Height="30"/>
<Button Content="添加" Width="60" Height="30" Click="Button_Click" />
</StackPanel>

</BlockUIContainer>
</Section>
</FlowDocument>
</RichTextBox>
<Button Name="GetText" Content="获取内容" HorizontalAlignment="Left" Margin="88,37,0,0" VerticalAlignment="Top" Width="75" Click="GetText_Click"/>

</Grid>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("这是添加按钮!");
}

private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
Process proc = new Process();
proc.StartInfo.FileName = "http://www.baidu.com";
proc.Start();
}

/// <summary>
/// 获取内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GetText_Click(object sender, RoutedEventArgs e)
{
TextRange range = new TextRange(rtbContent.Document.ContentStart, rtbContent.Document.ContentEnd);
string contentTxt = range.Text;//纯文本内容
MessageBox.Show(contentTxt);
}