title: How can I get my TextBlock to move across the screen at a steady pace no matter the length of its text?
body: <p>I'm trying to create some scrolling text (a marquee) in a desktop application. I originally tried to use timer ticks but as the time between ticks can be unreliable this resulted in jerky animation. I'm now trying to use WPF native animations instead.</p>
  
  <p>I have a <code>TextBlock</code> placed inside a <code>Canvas</code>. I can scroll the text easily with a standard <code>DoubleAnimation</code>.</p>
  
  <pre><code>var MyAnimation = new DoubleAnimation()
  {
      From = this.ActualWidth,
      To = -MyTextBlock.ActualWidth,
      Duration = new Duration(TimeSpan.FromMilliseconds(15000))
  };
  
  MyTextBlock.BeginAnimation(Canvas.LeftProperty, MyAnimation);
  </code></pre>
  
  <p>This causes my default textblock to scroll across the screen at a readable speed  until it disappears.</p>
  
  <p>My problem is that the length of the text inside the textblock will periodically change. This causes an issue as smaller text now goes across the screen very slowly but large text flies past. This is because the duration is unchanged.</p>
  
  <p>I will know the width in pixels of both the application and the textblock at runtime. I tried to calculate some ratio between the two that would let me work out the appropriate duration but it's hopelessly inaccurate.</p>
  
  <pre><code>var span = Convert.ToInt32((((double)textwidth / appwidth) * 1000) * 10);
  ...
  Duration = new Duration(TimeSpan.FromMilliseconds(span))
  </code></pre>
  
  <p><strong>Is there a recommended way to get textblock to move across the screen at a steady pace no matter the length of the text?</strong></p>
  
  <p>Here is a visual example of what I mean.<br>
  Observe what it looks like when the text length is increased but the duration is left the same. The longer text moves faster.<br>
  <a href="https://i.stack.imgur.com/EjHXb.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EjHXb.gif" alt="enter image description here"></a></p>
  
  <p>This is what it looks like when I've manually adjusted the duration to a longer period.<br>
  <a href="https://i.stack.imgur.com/QRLbz.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QRLbz.gif" alt="enter image description here"></a></p>
  
  <p>I can't figure out how you'd calculate the duration automatically based on the width of the app and the length of the text.</p>
  
  <p>(Ignore the jerky scrolling, it's a 15fps GIF, it's actually buttery smooth)</p>