Silerlight中利用TextBlock滚动显示

<Canvas x:Name="EventMovePanel" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1"  Margin="5,2">
    <Canvas.Clip>
        <RectangleGeometry RadiusX="0" RadiusY="0" Rect="0,0, 500,500" />
    </Canvas.Clip>
    <Canvas.Triggers>
        <EventTrigger RoutedEvent="Canvas.Loaded" >
            <BeginStoryboard>
                <Storyboard x:Name="animationGymQuality" Storyboard.TargetProperty="(Canvas.Top)" RepeatBehavior="Forever" >
                    <DoubleAnimation Storyboard.TargetName="txtGymQuality" From="100" To="0" Duration="0:0:20"  />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Canvas.Triggers>
    <TextBlock x:Name="txtGymQuality" HorizontalAlignment="Center" FontFamily="Arial" FontSize="12" Foreground="Orange" Text="" Canvas.Top="50" TextWrapping="NoWrap"/>
</Canvas>

 

这里是滚动文字的代码,主要的就是StoryBoard里的属性设置:

Storyboard.TargetProperty="(Canvas.Top)" From="100" To="0" Duration="0:0:20"

同理,其他方向如下设置即可:

Storyboard.TargetProperty="(Canvas.Top)" From="0" To="100" Duration="0:0:20"   //从上往下滚动

Storyboard.TargetProperty="(Canvas.Left)" From="0" To="100" Duration="0:0:20"   //从左往右滚动

Storyboard.TargetProperty="(Canvas.Left)" From="100" To="0" Duration="0:0:20"   //从右往左滚动

 

滚动内容有所变化时需要改变动画的From 与To

       void changeSizeOfQualtiyPanel()
       {
           txtGymQuality.Width = this.EventMovePanel.ActualWidth;
           DoubleAnimation animation = this.animationGymQuality.Children[0] as DoubleAnimation;
           if (animation != null)
           {
               double from = this.EventMovePanel.ActualHeight;
               double to = 0 - this.txtGymQuality.ActualHeight;
               animation.From = from;
               animation.To = to;
               animation.Duration = new Duration(TimeSpan.FromSeconds((from - to) / 50));
               this.EventMovePanel.Clip = new RectangleGeometry() { Rect = new Rect(new Point(0, 0), new Size(this.EventMovePanel.ActualWidth, this.EventMovePanel.ActualHeight)) };

               Canvas.SetTop(txtGymQuality, from);
           }
       }

posted on 2011-03-29 14:09  elaborateday  阅读(558)  评论(0编辑  收藏  举报