WPF 超长文本的来回滚动

当较小的地方显示超长的文本时  实现左右来回滚动

 

引入命名空间

xmlns:Utility="clr-namespace:Test.Wpf.Utility"

 实现

<ScrollViewer Name="slv" 
          IsHitTestVisible="False"
          Background="#05FFFFFF"
          Margin="2,10,2,0"
          VerticalScrollBarVisibility="Hidden"
          HorizontalScrollBarVisibility="Hidden">
    <Label Content="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
       VerticalContentAlignment="Center" 
       HorizontalContentAlignment="Center" 
       FontSize="18" 
       FontFamily="Microsoft YaHei" 
       Foreground="#FFFFFFFF" 
       Margin="5,0,5,0">
        <Label.Triggers>
            <EventTrigger RoutedEvent="Label.Loaded">
                <BeginStoryboard HandoffBehavior="Compose">
                    <Storyboard>
                        <DoubleAnimation From="0" 
                            To="{Binding ElementName=slv,Path=ScrollableWidth}"
                            Duration="0:0:2"
                            Storyboard.TargetName="slv"
                            AutoReverse="True"
                            RepeatBehavior="Forever"
                            BeginTime="0:0:0.2"
                            Storyboard.TargetProperty="(Utility:ScrollViewerBehavior.HorizontalOffset)">
                        </DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Label.Triggers>
    </Label>
</ScrollViewer>

ScrollViewerBehavior类

using System.Windows;
using System.Windows.Controls;

namespace Test.Wpf.Utility
{
    public static class ScrollViewerBehavior
    {
        public static readonly DependencyProperty HorizontalOffsetProperty = DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(ScrollViewerBehavior), new UIPropertyMetadata(0.0, OnHorizontalOffsetChanged));
        public static void SetHorizontalOffset(FrameworkElement target, double value)
        {
            target.SetValue(HorizontalOffsetProperty, value);
        }
        public static double GetHorizontalOffset(FrameworkElement target)
        {
            return (double)target.GetValue(HorizontalOffsetProperty);
        }
        private static void OnHorizontalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            var view = target as ScrollViewer;
            if (view != null)
            {
                view.ScrollToHorizontalOffset((double)e.NewValue);
            }
        }

        public static readonly DependencyProperty VerticalOffsetProperty = DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerBehavior), new UIPropertyMetadata(0.0, OnVerticalOffsetChanged));
        public static void SetVerticalOffset(FrameworkElement target, double value)
        {
            target.SetValue(VerticalOffsetProperty, value);
        }
        public static double GetVerticalOffset(FrameworkElement target)
        {
            return (double)target.GetValue(VerticalOffsetProperty);
        }
        private static void OnVerticalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            var view = target as ScrollViewer;
            if (view != null)
            {
                view.ScrollToVerticalOffset((double)e.NewValue);
            }
        }
    }
}

 

WPF中,可以使用鼠标指向超长文本动画滚动的方式来提升用户体验。首先,我们可以创建一个超长文本框,使其内容超出显示区域。然后,通过监测鼠标指向文本框的事件来触发滚动动画。 首先,在XAML中创建一个文本框,并指定其长度超出显示区域,例如设置为宽度为500px。然后,为文本框添加一个鼠标进入的事件处理程序。 ```xml <TextBlock Width="500" Text="超长文本内容" MouseEnter="ScrollTextBlock_MouseEnter" /> ``` 接下来,在C#代码中编写鼠标进入事件的处理程序。在事件处理程序中,我们可以使用WPF的动画功能来实现滚动效果。通过将文本框的Margin属性在一段时间内逐渐改变,从而实现动画滚动效果。 ```csharp private void ScrollTextBlock_MouseEnter(object sender, MouseEventArgs e) { DoubleAnimation animation = new DoubleAnimation(); animation.From = 0; animation.To = -textBlock.ActualWidth; animation.Duration = new Duration(TimeSpan.FromSeconds(5)); // 持续时间为5秒 textBlock.BeginAnimation(TextBlock.MarginProperty, new ThicknessAnimation { From = new Thickness(0), To = new Thickness(animation.To.Value, 0, 0, 0), Duration = animation.Duration }); } ``` 在上述示例代码中,我们创建了一个DoubleAnimation来改变滚动距离,从0到负的文本框宽度。然后,将这个动画应用到文本框的Margin属性上,通过改变Margin的Left值来实现滚动效果。 总结起来,以上就是使用WPF实现鼠标指向超长文本动画滚动的简单示例。通过这种方式,可以让用户在鼠标指向时自动滚动文本内容,提升用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值