silverlight 滚动文字

来源:互联网 发布:淘宝饰品店发展前景 编辑:程序博客网 时间:2024/06/05 19:14
<UserControl x:Class="DB_Typhoon.elements.rollbottom"
    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">


    <ScrollViewer HorizontalScrollBarVisibility="Hidden" Grid.Row="1"
    VerticalScrollBarVisibility="Hidden" BorderThickness="0" Height="36">
        <Canvas Loaded="MarqueeControl_Loaded" Width="592" HorizontalAlignment="Left">
            <Canvas.Resources>
                <Storyboard x:Name="sb">
                    <DoubleAnimation x:Name="da" BeginTime="00:00:04"
                    Storyboard.TargetName="txtLorem"
                    Storyboard.TargetProperty="(Canvas.Left)"
                    From="0" RepeatBehavior="Forever"/>
                </Storyboard>
            </Canvas.Resources>
            <TextBlock x:Name="txtLorem" Width="596" Height="33" VerticalAlignment="Center" Style="{StaticResource Block1}" Margin="0,0,0,0" />
        </Canvas>
    </ScrollViewer>


</UserControl>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace DB_Typhoon.elements
{
    public partial class rollbottom : UserControl
    {
        public rollbottom(string text)
        {
            InitializeComponent();
            this.txtLorem.Text = text;
        }


        private void MarqueeControl_Loaded(object sender, RoutedEventArgs e)
        {
            var canvas = sender as Canvas;
            if (txtLorem.ActualWidth <= canvas.Width) return;
            const double speed = 50;
            da.To = -txtLorem.ActualWidth;
            da.Duration = new Duration(TimeSpan.FromSeconds(txtLorem.ActualWidth / speed));
            sb.Begin();
        }
    }
}