c# WPF滚动动画的RichTextBox

本文关键字:RichTextBox 动画 滚动 WPF | 更新日期: 2023-09-27 18:19:09

我需要在c# . net中使用WPF实现RichTextBox控件上的滚动条动画。

当我点击一个按钮时,动画应该把我带到文本的末尾。为此,我使用ScrollToEnd()方法,但我不知道如何执行动画。我用BeginAnimation()方法尝试了一些东西,但是没有效果。

如果你们中有人知道的话,那就太棒了。谢谢!

My XAML:

<Window x:Class="TestWpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="177.811" Width="338.88">
<Grid Margin="0,0,2,-1">
    <RichTextBox x:Name="rtb" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="319" ScrollViewer.VerticalScrollBarVisibility="Visible">
        <FlowDocument>
            <Paragraph>
                <Run Text="RichTextBoxR
                     ichTextBoxRichTextBoxRichTextBoxRic
                     hTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTe
                     xtBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRic
                     hTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextB
                     oxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRi
                     extBoxRichTextBoxRichTextBoxRichTextBoxRichTe
                     xtBoxRichTextBoxRichTextBoxRichTextBoxRichTextBoxRichTe
                     xtBoxRichTextBoxRich
                     TextBoxRichT
                     extBox"/>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
    <Button Content="Button" HorizontalAlignment="Left" Margin="122,121,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>

我在XAML.cs:

中的按钮单击方法
private void Button_Click_1(object sender, RoutedEventArgs e) { rtb.ScrollToEnd(); }

非常感谢!致以最亲切的问候。

c# WPF滚动动画的RichTextBox

我找到了解决方案。我从RichTextBox属性中创建了一个新类,并添加了一个Dependencyproperty以使其工作:

class ExtRichTextBox : RichTextBox
{
    public static DependencyProperty CurrentVerticalOffsetProperty =
        DependencyProperty.Register("CurrentVerticalOffset", typeof(double), typeof(ExtRichTextBox), new PropertyMetadata(new PropertyChangedCallback(OnVerticalChanged)));
    private static void OnVerticalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ExtRichTextBox extRtb = d as ExtRichTextBox;
        extRtb.ScrollToVerticalOffset((double)e.NewValue);
    }
    public double CurrentVerticalOffset
    {
        get { return (double)this.GetValue(CurrentVerticalOffsetProperty); }
        set { this.SetValue(CurrentVerticalOffsetProperty, value); }
    }
}

当然你需要用新的控件替换旧的控件然后使用storyboard来动画滚动条:

        private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        DoubleAnimation vertAnim = new DoubleAnimation();
        vertAnim.From = rtb.VerticalOffset;
        vertAnim.To = 100;
        vertAnim.DecelerationRatio = .2;
        vertAnim.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
        Storyboard sb = new Storyboard();
        sb.Children.Add(vertAnim);
        Storyboard.SetTarget(vertAnim, rtb);
        Storyboard.SetTargetProperty(vertAnim, new PropertyPath(ExtRichTextBox.CurrentVerticalOffsetProperty));
        sb.Begin();
    }

只是替换给vertAnim的坐标。