title: WPF Grid with a dynamic number of columns
body: <p>I am quite new to WPF, and am struggling with accomplishing a visual effect - I would like to have a two-dimensional grid of objects with a data-driven number of columns. I am trying to go with MVVM with zero code-behind.  I have had a look at several posts in this regard, and have come up with the following:</p>
  
  <pre><code>&lt;UserControl x:Class="Demo.Views.SideBySideStackPanelView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:views="clr-namespace:Demo.Views"
               xmlns:viewModels="clr-namespace:Demo.ViewModels"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;UserControl.Resources&gt;
          &lt;DataTemplate x:Key="ColumnTemplate"&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="400"/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="*" SharedSizeGroup="TreeView"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="*" SharedSizeGroup="DetailView"/&gt;
                  &lt;/Grid.RowDefinitions&gt;
  
                  &lt;DockPanel Grid.Row="0" LastChildFill="True"&gt;
                      &lt;TextBlock Text="{Binding Name}" DockPanel.Dock="Left"/&gt;
                      &lt;Button Command="{Binding Close}" DockPanel.Dock="Right" HorizontalAlignment="Right"&gt;
                          &lt;Image Width="10" Height="10" Source="/Demo;component/Images/Close.png" /&gt;
                      &lt;/Button&gt;
                  &lt;/DockPanel&gt;
  
                  &lt;views:TreeView Grid.Row="1"/&gt;
  
                  &lt;GridSplitter 
                      ResizeDirection="Rows" 
                      VerticalAlignment="Center"
                      HorizontalAlignment="Stretch"
                      Height="5"
                      Grid.Row="2"
                      Name="sideBySideSplitter"/&gt;
  
                  &lt;views:TreeDetail Grid.Row="2"/&gt;
  
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid d:DataContext="{d:DesignInstance viewModels:MainWindowViewModel}" Grid.IsSharedSizeScope="True"&gt;
          &lt;ItemsControl ItemsSource="{Binding PluginItem}" ItemTemplate="{StaticResource ColumnTemplate}"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
          &lt;/ItemsControl&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>The TreeView view is a basic bound TreeView and the TreeDetail view is a simple bound Grid.</p>
  
  <p>The control does render my content more-or-less as desired, but when I interact with the tree view that is in the upper part of the column the control expands vertically rather than a scroll bar showing up in the tree. The GridSplitter doesn't work at all; in fact it appears to render in the middle of the detail object.</p>
  
  <p>I have a similar DataTemplate in a TabControl.ContentTemplate and it works as desired. Moving the GridSplitter causes scroll bars to show up on either side when necessary and opening up TreeView items causes scroll bars to show up in that control.</p>
  
  <p>What I am after is an Excel-like presentation where the user can control the sizes of the cells with horizontal and vertical "splitters" and the user objects within the cells scroll within the cell as necessary. I can live with fixed-size columns for now.</p>
  
  <p>I would appreciate any help provided. Thanks.</p>