2021-08-31 WPF控件专题 RichTextBox 控件详解

Posted 微软MVP Eleven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-08-31 WPF控件专题 RichTextBox 控件详解相关的知识,希望对你有一定的参考价值。

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);
}

以上是关于2021-08-31 WPF控件专题 RichTextBox 控件详解的主要内容,如果未能解决你的问题,请参考以下文章

2021-08-19 WPF控件专题 TabControl 控件详解

2021-08-09 WPF控件专题 Button控件详解

2021-08-17 WPF控件专题 Groupbox 控件详解

2021-08-10 WPF控件专题 Image控件详解

2021-08-14 WPF控件专题 Calendar 控件详解

2021-08-16 WPF控件专题 StackPanel 控件详解