[WPF] 跑馬燈範例

  • 1938
  • 0

摘要:[WPF] 跑馬燈範例

示範文字A從視窗最右邊,跑到最左邊消失,跑一次為5秒鐘,並重複這個動作, 且當視窗大小改變時,會重新讓文從最右邊開始跑。
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" x:Name="win">
    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.SizeChanged">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="a" 
                                         Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)"
                                                 From="{Binding Width, ElementName=win}"
                                                 To="-10"
                                                 Duration="0:0:5"
                                                 RepeatBehavior="Forever">

                        </DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Window.Triggers>
    
    <Grid>
        <TextBlock x:Name="a" Text="A">
            <TextBlock.RenderTransform>
                <TranslateTransform />
            </TextBlock.RenderTransform>

        </TextBlock>
    </Grid>
</Window>