WPF RichTextBox滚动条自动滚动实例、文本自动滚动实例

说明:
1.后台代码添加测试 数据

2.使用 richTextBox.ScrollToVerticalOffset()方法,滚动竖直方向滚动条位置

3.使用定时器DispatcherTimer,修改页面显示数据

4.自己计算处理,已经滚动的高度位置

Xaml代码:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="205*"/>
        <ColumnDefinition Width="87*"/>
    </Grid.ColumnDefinitions>
    <Button x:Name="button" Content="开始播放"
            HorizontalAlignment="Left" Margin="2,36,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Height="29" Click="button_Click"/>
    <RichTextBox x:Name="richTextBox" 
                    HorizontalAlignment="Left" Height="209" Margin="10,36,0,0" VerticalAlignment="Top" Width="170">
    </RichTextBox>
</Grid>

后台添加测试数据代码:

public text4()
{
    InitializeComponent();

    richTextBox.Document = doc;
    richTextBox.FontSize = 20;
    //添加内容
    appendLine(null, "从你的全世界路过");
    appendLine("one", "海上生明月");
    appendLine(null, "从你的全世界路过");
    appendLine(null, "天涯共此时");
    appendLine("two", "张三丰");
    appendLine(null, "从你的全世界路过");
    appendLine(null, "鲁迅先生");
    appendLine(null,null);
}
FlowDocument doc = new FlowDocument();
private void appendLine(string name, string line)
{
    Paragraph p = new Paragraph();
    if (string.IsNullOrEmpty(name) == false)
        doc.RegisterName(name, p);
    Run r = new Run(line);
    p.TextAlignment = TextAlignment.Center;
    p.Inlines.Add(r);
    doc.Blocks.Add(p);
}

定时器显示控制代码:

int pIndex = 0;
double curTop = 0;
private void button_Click(object sender, RoutedEventArgs e)
{
    //定时控制内容显示和滚动条位置
    DispatcherTimer _timer = new DispatcherTimer();
    _timer.Interval = TimeSpan.FromSeconds(1);
    _timer.Tick += (st, et) =>
    {
        //获取指定行的内容
        BlockCollection col = richTextBox.Document.Blocks;
        int index = 0;
        TextElement prev = null;
        foreach (TextElement item in col)
        {
            //修改当前行的样式
            if (index == pIndex)
            {
                AlterStyle(item, prev);
            }
            index++;
            prev = item;
        }
        pIndex++;
    };
    _timer.Start();
}
private void AlterStyle(TextElement item, TextElement prev)
{
    //当前行
    Paragraph cP = item as Paragraph;
    cP.Foreground = Brushes.Red;
    TextRange range = new TextRange(cP.ContentStart, cP.ContentEnd);
    //滚动位置控制
    if (pIndex > 0 && range.Text.Length > 0)
    {
        //上一行,样式回调
        if (prev != null)
        {
            prev.Foreground = Brushes.Black;
        }
        curTop += range.Text.Length > 7 ? 40 : 20;
        curTop += 20;
        richTextBox.ScrollToVerticalOffset(curTop);
    }
}

运行结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPFRichTextBox控件提供了多种自定义滚动条的方式,可以使用XAML来定义样式和模板。以下是一个简单的示例,展示了如何自定义RichTextBox滚动条样式: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <!--定义一个自定义的ScrollBar样式--> <Style x:Key="CustomScrollBar" TargetType="{x:Type ScrollBar}"> <Setter Property="Background" Value="LightGray"/> <Setter Property="Width" Value="20"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ScrollBar}"> <Grid> <!--定义滚动条的轨道--> <Border Background="{TemplateBinding Background}" CornerRadius="10"/> <!--定义滚动条的滑块--> <Border Name="PART_Track" Background="DarkGray" CornerRadius="10"/> <RepeatButton Name="PART_UpButton" Style="{StaticResource ScrollBarLineButton}" Command="ScrollBar.LineUpCommand"/> <RepeatButton Name="PART_DownButton" Style="{StaticResource ScrollBarLineButton}" Command="ScrollBar.LineDownCommand"/> <Thumb Name="PART_Thumb" Style="{StaticResource ScrollBarThumb}" Margin="2"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <RichTextBox ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" VerticalScrollBarStyle="{StaticResource CustomScrollBar}" HorizontalScrollBarStyle="{StaticResource CustomScrollBar}"> <FlowDocument> <Paragraph> This is a sample text! </Paragraph> </FlowDocument> </RichTextBox> </Grid> </Window> ``` 在这个示例中,我们定义了一个名为CustomScrollBar的自定义样式,包含了滚动条的轨道、滑块和按钮等控件。然后,我们将RichTextBox的VerticalScrollBarStyle和HorizontalScrollBarStyle属性分别设置为CustomScrollBar,即可应用自定义的滚动条样式。 需要注意的是,这只是一个简单的示例,实际中可能需要根据具体需求进行更加复杂的滚动条自定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值