2013-08-20 43 views
1

我想實現一個簡單的標籤滾動更新寬度(發現這個想法在這裏:Scroller StackOverflow我有一個標籤,我想與DoubleAnimation是一流的幫助這樣的動畫:WPF標籤如何正確動畫

在構造函數中我加載的實現事件:

public Web() 
    { 
     InitializeComponent(); 
     SetDefaultBrowser(); 
     SetDefaultWebsite(); 
     //TextBlockSong.Text = GetSongName(GetBrowserName(), GetWebsiteName()); 
     Loaded += Window1_Loaded; 

    } 

事件:

void Window1_Loaded(object sender, RoutedEventArgs e) 
    { 
     DoubleAnimation doubleAnimation = new DoubleAnimation(); 
     doubleAnimation.From = -LabelNameSong.ActualWidth; 
     doubleAnimation.To = canMain.ActualWidth; 
     doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; 
     doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10")); 
     LabelNameSong.BeginAnimation(Canvas.RightProperty, doubleAnimation); 
    } 

一切正常,直到我更新我的LabelNameSong內容。 LabelNameSong寬度與之前保持相同,並且我的動畫無法從一開始就正常工作(使用更新的文本)。

更新我LabelNameSong與ListBox_SelectionChanged事件:

  private void ListBoxWebsite_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     double k = LabelNameSong.Width; 
     double z = LabelNameSong.ActualWidth; 
     LabelWebsiteName.Content = "Now on " + GetWebsiteName(); 
     LabelNameSong.Content = GetSongName(GetBrowserName(), GetWebsiteName()); 

     k = LabelNameSong.Width; 
     z = LabelNameSong.ActualWidth; 
    } 

我用這些測量寬度,並發現它不更新的LabelNameSong寬度。我是新來的,甚至不知道它是否應該。

這是我的XAML:

 <Canvas Background="White" Margin="0,182,58,0" > 
     <Canvas ClipToBounds="True" Name="canMain" Height="80" Width="346" > 
      <Label FontSize="25" Foreground="#666666" Name="LabelNameSong" Canvas.Top="27" Height="30" Width="Auto" Content="This is very long text, I am testing it!" FontFamily="Calibri Light"/> 
     </Canvas> 
     </Canvas> 

所以我的問題是,我還能有什麼更新我LabelNameSong的寬度,我應該如何重新調用Window1_Loaded的事件,使新實例DoubleAnimation可以使用更新的ActualWidth?

 doubleAnimation.From = -LabelNameSong.ActualWidth; 
     doubleAnimation.To = canMain.ActualWidth; 

謝謝。

回答

2

可以使用觸發器只是包裝動畫邏輯到一個方法,並從Loaded事件和任何其他事件調用你需要

private void CreateAnimation() 
{ 
    DoubleAnimation doubleAnimation = new DoubleAnimation(); 
    doubleAnimation.From = -LabelNameSong.ActualWidth; 
    doubleAnimation.To = canMain.ActualWidth; 
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; 
    doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10")); 
    LabelNameSong.BeginAnimation(Canvas.RightProperty, doubleAnimation); 
} 

void Window1_Loaded(object sender, RoutedEventArgs e) 
{ 
    CreateAnimation(); 
} 

private void LabelNameSong_SizeChanged(object sender, RoutedEventArgs e) 
{ 
    CreateAnimation(); 
} 

但你可能可以做到這一切的XAML和擺脫所有的代碼背後。

在動畫下面的例子將開始加載,當大小的變化

實例重啓:

<Label x:Name="LabelNameSong" Content="Hello" > 
    <Label.Resources> 
     <Storyboard x:Key="scroll"> 
      <DoubleAnimation To="{Binding ActualWidth, ElementName=LabelNameSong}" Duration="00:00:10" 
       Storyboard.TargetProperty="(Canvas.Right)" 
       Storyboard.TargetName="LabelNameSong" 
       RepeatBehavior="Forever"/> 
     </Storyboard> 
    </Label.Resources> 

    <Label.Triggers> 
     <EventTrigger RoutedEvent="Label.Loaded"> 
      <BeginStoryboard Storyboard="{StaticResource scroll}" /> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Label.SizeChanged"> 
      <BeginStoryboard Storyboard="{StaticResource scroll}" /> 
     </EventTrigger> 
    </Label.Triggers> 
</Label> 
+0

你是生命的救星好先生。謝謝!順便說一句,你可以在我的代碼中看到我的第一個文本位置是-ActualWidth,並沿着畫布的大小,並重復。這意味着它是這樣的:http://www.codeproject.com/KB/WPF/WPFAnimation1/marq1.gif - RightToLeft示例。我喜歡你的XAML方法,但是你能給我一個提示,我怎樣才能使用DoubleAnimation From作爲負面的位置?謝謝 –

+0

如果yu在下面的鏈接中檢查我的答案,那麼有一個轉換器可以做到這一點,該答案中的滾動有點不同,但轉換器是你需要的。 http://stackoverflow.com/questions/15323163/wpf-marquee-text-animation –