title: WPF ContentControl width grows but doesn't shrink when wrapped in a ScrollViewer
body: <p>I'm trying to figure out how to make my <code>ContentControl</code> to correctly scroll Horizontally (Vertically its fine at the moment). By correctly i mean i would like to see the content to stretch (expand infinitely) while having minimum sizes to which a scrollbar would appear in order for the content not to overflow behind the <code>ContentControl</code>'s area, so here's a quick introduction:</p>
  
  <p>The main window is structured in this way:</p>
  
  <ul>
  <li>Grid (2 columns of .3* and .7*)
  <ul>
  <li>Border
  <ul>
  <li>Grid (7 rows, one set to * where ContentControl is)
  <ul>
  <li>ScrollViewer with StackPanel (purely for test) wrapping a ContentControl that has Auto Width</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p><code>ContentControl</code>'s Template:</p>
  
  <ul>
  <li>Grid (Width set to UserControl's ActualWidth, 6 rows with one set to Auto where ItemsControl go
  <ul>
  <li>ItemsControl that describes an ItemTemplate of a type DataTemplate which contains a Grid inside of which i have a DataGrid</li>
  </ul></li>
  </ul>
  
  <p>The actual problem is that the ContentControl grows as you resize the window, but does not shrink with window resize.</p>
  
  <p>Main View XAML (truncated for clarity):</p>
  
  <pre><code>&lt;ScrollViewer Grid.Row="5" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"&gt;
          &lt;ContentControl Grid.Row="5" Background="Transparent"  Focusable="False" Margin="0,5,0,0"
                              Content="{Binding CurrentSection}" ContentTemplateSelector="{StaticResource templateSelector}/&gt;
  &lt;/ScrollViewer&gt;
  </code></pre>
  
  <p>Tempate XAML (truncated for clarity):</p>
  
  <pre><code>&lt;Grid&gt;
  ...
      &lt;ItemsControl Grid.Row="4" ItemsSource="{Binding Data.QualifyingDistributionsDividends}" x:Name="QualifyingItemsControl"&gt;
          &lt;ItemsControl.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;Grid x:Name="DTLayoutGrid"&gt;
                      ...
                      &lt;Grid Grid.Row="1" x:Name="DataLayout" Width="{Binding ElementName=DTLayoutGrid, Path=ActualWidth}" HorizontalAlignment="Stretch"&gt;
                          ...
                          &lt;DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="8" HorizontalScrollBarVisibility="Disabled"
                                    ItemsSource="{Binding Payments}" Style="{StaticResource DataGridStyle}" CellStyle="{StaticResource DataGridNormalCellStyle}"&gt;
                          &lt;/DataGrid&gt;
                      &lt;/Grid&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/ItemsControl.ItemTemplate&gt;
      &lt;/ItemsControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>So what happens? <code>Datagrid</code> assumes width of the entire <code>DataTemplate</code> (well its underlying controls that are set to be <code>DataTemplates</code> size, then * column assumes all empty space. When you try to resize the entire window that holds this code it will grow correctly, expanding the * column but it seems shrinking is not "registered" and it keeps the size you expanded it to, applies a scrollbar over that and forgets about it.</p>
  
  <p>What i've tried so far was to set widths for <code>ItemsControl</code>, its underlying parents like Grid etc, also setting size to <code>ContentControl</code>, <code>StackPanel</code>, <code>ScrollViewer</code> and parent Grid of that.
  I've also tried using <code>scrollviewer</code>s directly on the <code>Datagrid</code> which produces an epileptic "1 million resizes a second" scenario. I've also played around with <code>HorizontalAlignments</code> 
  Under certain situations i DID managed to get the horizontal scrollbar to appear correctly but unfortunately that makes my <code>DataGrid</code>'s * column to assume <code>Auto</code> Width rather then <code>Star</code> so <code>DataGrid</code> starts having an empty area to the right (unacceptable unfortunately...)</p>
  
  <p>I understand that in order for horizontal scrollbar to work the parent or child of the scrollviewer needs Width set, i guess i can't work out where exactly do i need to restrict it. DataGrids NEED to infinitely expand with the main window while having first column fill all the available space.
  Do let me now if you need more information on this and I will gladly answer.</p>