Creating marquee/scrolling text in WPF

mar1

A WPF marquee is a scrolling piece of text displayed either horizontally across or vertically down your container. Here i show you how to make a scrolling marquee using Canvas and TextBlock. I will show only left to right in code. However, you can achieve right to left or vertical marquee text just by changing some portion of code using the same technique.

The following three techniques are used to achieve marquee animation:

  • Negative Positioning in canvas: If we set negative value to any of the positioning property of Canvas: Left, Right, Top and Bottom for an UIelement , Canvas draws that Uielement outside of Canvas area.
  • ClipToBounds property of canvas: if you set the value of the ClipToBounds property to true, Canvas will clip its children when they go outside of Canvas.
  • Changing Position of Uielement continuously: By changing continuously any of the canvas positioning property Left, right, Top, Bottom we can get moving text.

How is Left to right marquee text implemented in this sample?

Here a canvas is used as the container of marquee text. To move the text outside of canvas negative Left property of canvas is used. However, if someone uses negative left property of canvas, it will go outside of its parent container but will still remain visible outside of the parent container. To make marquee text invisible outside of canvas CliptoBound property is used. Then using double animation left property of canvas for that element is being changed continuously to get moving text. The whole thing is done using the following code.

   1: void Window1_Loaded(object sender, RoutedEventArgs e)
   2: {
   3:     DoubleAnimation doubleAnimation = new DoubleAnimation();
   4:     doubleAnimation.From = -tbmarquee.ActualWidth;
   5:     doubleAnimation.To = canMain.ActualWidth;
   6:     doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
   7:     doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
   8:     tbmarquee.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
   9: }

For Left to right marquee: use Left Property of Canvas

For right to left marquee: use Right Property of Canvas

For upward marquee: use Bottom Property of Canvas

For Downward marquee: use Top Property of Canvas

The used XAML for this is in the following

   1: <Window x:Class="MarqueText.Window1"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     Title="Window1" Height="500" Width="700" >
   5:     <Canvas Background="White" >
   6:         <Canvas Margin="50" Canvas.Left="150" Canvas.Top="100" ClipToBounds="True" Name="canMain" Background="Red"   Height="100" Width="300"  >
   7:             <TextBlock Margin="0,40" FontSize="25" Name="tbmarquee">Bangladesh</TextBlock>
   8:         </Canvas>
   9:     </Canvas>
  10: </Window>

You can download the sample code from here. Hope this will save some of your time.

7 Comments

  • So little code, and still good looking. Nice job. Maybe you have a control version of it too?

  • Yes, i have a control version. I will post it to Code Project shortly. Thanks..............

  • One quick point: for scrolling toward the right, you not only use the RightProperty of Canvas, but you must also inverse both the To and From values (compared to the LeftPropoerty as shown in the sample code). &nbsp;I haven't experimented with up/down yet but I suspect you need to use something other than ActualWidth...

  • For Left, right, Up and down text marquee, you can look at http://www.codeproject.com/KB/WPF/WPFAnimation1.aspx

  • One quick point:i need to stop vertical scrolled hyperlinks when i place mouse at event in xaml.

    is there any way to solve this this question?

  • How to get the data from database, and then to scroll them?

    Regards

    Drilon

  • This article is very good, and I want to know how to make the scrolling text wrap? That means if the scrolling text should be like this, &quot;this is a very long string&quot;, supposing scrolling from right to left, whehn the &quot;this is a&quot; scrolling out of canvas, can &quot;this is a&quot; appending to the end of the scrolling text?

Comments have been disabled for this content.