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;
謝謝。
你是生命的救星好先生。謝謝!順便說一句,你可以在我的代碼中看到我的第一個文本位置是-ActualWidth,並沿着畫布的大小,並重復。這意味着它是這樣的:http://www.codeproject.com/KB/WPF/WPFAnimation1/marq1.gif - RightToLeft示例。我喜歡你的XAML方法,但是你能給我一個提示,我怎樣才能使用DoubleAnimation From作爲負面的位置?謝謝 –
如果yu在下面的鏈接中檢查我的答案,那麼有一個轉換器可以做到這一點,該答案中的滾動有點不同,但轉換器是你需要的。 http://stackoverflow.com/questions/15323163/wpf-marquee-text-animation –