Click here to Skip to main content
14,979,231 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a WPF application which has a textblock at the bottom of the page. I want to animate this block so it scrolls along the bottom. I need to do this in code so that when it scrolls of screen it will change position to the other side when it's canvas is less than 0 - it's width as I also change the text at this point. This will loop. I did the exact same thing in a silverlight application with a storyboard and it worked perfectly. The code I used is as follows:

private Storyboard _newsScrollTimer = new Storyboard();
_newsScrollTimer.Duration = TimeSpan.FromMilliseconds(0);
               _newsScrollTimer.Completed += new EventHandler(Animation);
               _newsScrollTimer.Begin();
private void Animation(object sender, EventArgs e)
        {
            try
            {
                //Moves the news item using its canvas property.
                NewsItem.SetValue(Canvas.LeftProperty, (double)NewsItem.GetValue(Canvas.LeftProperty) - 1.5);
_newsScrollTimer.Begin();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


This works perfectly fine in my silverlight app, however it won't work at all in WPF. If I change the timespan value to 1 it will work for a few seconds and then stop suddenly. It stops at a different place each time. How do I modify this code so it will work for WPF. Alternatively could you tell me another way to animate a textblock smoothly. I tried using a dispatch timer instead which works, but the animation is jerky in places. Thanks in advance.
Posted
Updated 18-Feb-11 0:56am
v2

1 solution

You could install Expression Blend from Microsoft and then do it all in a XAML storyboard. I have done things like this in WPF using the XAML directly with good success. The XAML code you get will be a good example and then you can tweak it manually.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900