0

我需要在 C# .NET 中使用 WPF 实现 RichTextBox 控件上的 ScrollBar 动画。

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

如果你们中的任何人有任何想法,那就太棒了。谢谢!

我的 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(); }

非常感谢 !此致。

4

1 回答 1

1

好的,我找到了解决方案。我从 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); }
    }
}

当然,您需要用新控件替换旧控件,然后使用情节提要为滚动条设置动画:

        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.To 属性提供的坐标即可滚动到您想要的位置。

于 2013-06-21T12:24:02.143 回答