title: Scrolling a ScrollViewer to the bottom in WPF when a new line is added to a TextBlock in XAML
body: <p>I'm trying to keep the vertical scroll bar at the bottom (the latest entry), but at the moment, the scroll bar just stays in the same place, so as content is being added to the string, the scroll bar moves to the top. </p>
  
  <p>I know that I can use the <code>ServerScroll.ScrollToEnd()</code> property in my code-behind to move the bar to the end. But is there a way to automatically do this is xaml? (so that I don't have to call this property every time I add to the string).</p>
  
  <p><strong>XAML</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ScrollViewer Name="ServerScroll"
                VerticalScrollBarVisibility="Auto"&gt;
      &lt;TextBlock Name="serverConsole"
                 Margin="5"
                 Background="White"
                 TextWrapping="Wrap"/&gt;
  &lt;/ScrollViewer&gt;
  </code></pre>
  
  <p><strong>Code-behind</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>private void example_Click(object sender, RoutedEventArgs e)
  {
      ServerConsole += "asdf\r\n";      // binded to TextBlock
      ServerScroll.ScrollToEnd();    
  }
  </code></pre>