title: WPF - Change the color of an entire textblock line
body: <p>In my WPF Application I have a two textblocks which get filled from the code behind with a run. Every second line should have a different background color so it gets easier to read. Unfortunately, the lines are only dyed as far as they are written. But I want the background color to go over the entire line and not just the written area.</p>
  <p>Code:</p>
  <pre><code>         for (int i = 0; i &lt; GlobalSettings.prefixList.Count; i++)
           {
              runLeft = new Run(GlobalSettings.prefixList[i].prefix + &quot;\n&quot;);
              runRight = new Run(GlobalSettings.prefixList[i].amount + &quot;\n&quot;);
              
              if (i % 2 == 0)
              {
                  runLeft.Background = Brushes.Gray;
                  runRight.Background = Brushes.Gray;
              }
              else
              {
                  runLeft.Background = Brushes.LightGray;
                  runRight.Background = Brushes.LightGray;
              }
  
              tblock_StatisticsLeft.Inlines.Add(runLeft);
              tblock_StatisticsRight.Inlines.Add(runRight);
           }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Tmx6G.png" rel="nofollow noreferrer">Example Picture</a></p>
  <p>The two textblocks are seamlessly together in the middle so it would look like it is a single line in a single textblock. The spaces between the lines are negligible if this makes it easier.</p>
  <p>Is there a solution without using a textbox or richtextbox?</p>
  <p><strong>EDIT:</strong></p>
  <p>XAML Code:</p>
  <pre><code>&lt;UserControl x:Class=&quot;MuseKeyGenApp.UCStartUp&quot;
           xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
           xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
           xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
           xmlns:local=&quot;clr-namespace:MuseKeyGenApp&quot;
           mc:Ignorable=&quot;d&quot;   
           Background = &quot;#FF0069B4&quot;
           d:DesignHeight=&quot;500&quot; d:DesignWidth=&quot;800&quot;&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height=&quot;26&quot;/&gt;
          &lt;RowDefinition Height=&quot;0&quot;/&gt;
          &lt;RowDefinition Height=&quot;*&quot; MinHeight=&quot;80&quot;/&gt;
          &lt;RowDefinition Height=&quot;*&quot; MinHeight=&quot;80&quot;/&gt;
          &lt;RowDefinition Height=&quot;*&quot; MinHeight=&quot;80&quot;/&gt;
          &lt;RowDefinition Height=&quot;*&quot; MinHeight=&quot;80&quot;/&gt;
          &lt;RowDefinition Height=&quot;*&quot; MinHeight=&quot;80&quot;/&gt;
          &lt;RowDefinition Height=&quot;0&quot;/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width=&quot;1*&quot; MinWidth=&quot;150&quot;/&gt;
          &lt;ColumnDefinition Width=&quot;0.75*&quot; MinWidth=&quot;100&quot;/&gt;
          &lt;ColumnDefinition Width=&quot;0.75*&quot; MinWidth=&quot;100&quot;/&gt;
          &lt;ColumnDefinition Width=&quot;0.75*&quot; MinWidth=&quot;100&quot;/&gt;
          &lt;ColumnDefinition Width=&quot;0.75*&quot; MinWidth=&quot;100&quot;/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
  
      &lt;ScrollViewer Grid.Row=&quot;2&quot; Grid.RowSpan=&quot;3&quot; Grid.Column=&quot;1&quot; Grid.ColumnSpan=&quot;2&quot; Margin=&quot;10,35,12,12&quot; Name=&quot;sv_PrefixList&quot;&gt;
          &lt;Grid&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width=&quot;1*&quot;/&gt;
                  &lt;ColumnDefinition Width=&quot;1*&quot;/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBlock Grid.Column=&quot;0&quot; x:Name=&quot;tblock_StatisticsLeft&quot; HorizontalAlignment=&quot;Stretch&quot; LineHeight=&quot;20&quot; TextWrapping=&quot;Wrap&quot; Margin=&quot;0,0,0,0&quot; Text=&quot;TextBlock&quot; VerticalAlignment=&quot;Stretch&quot; TextAlignment=&quot;Left&quot;/&gt;
              &lt;TextBlock Grid.Column=&quot;1&quot; x:Name=&quot;tblock_StatisticsRight&quot; HorizontalAlignment=&quot;Stretch&quot; LineHeight=&quot;20&quot; TextWrapping=&quot;Wrap&quot; Margin=&quot;0,0,10,0&quot; Text=&quot;TextBlock&quot; VerticalAlignment=&quot;Stretch&quot; TextAlignment=&quot;Right&quot;/&gt;
          &lt;/Grid&gt;
      &lt;/ScrollViewer&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p>I left out all the other stuff of the control that is not relevant.</p>