title: WPF: MVVM Binding Two Dimensional Collection to GridView
body: <p>I'm looking for a way to bind a two dimensional collection to a <code>ListView</code> <code>GridView</code>.  So far I have been able to bind my data to nested <code>ItemsControl</code> controls that look like a grid.  However I would like it to behave like a <code>GridView</code> where the columns can be resized by the user.</p>
  
  <p>The view model looks like this:</p>
  
  <pre><code>using GalaSoft.MvvmLight;
  using System.Collections.ObjectModel;
  
  namespace BindingTest
  {
      public class ViewModel : ViewModelBase
      {
          public ObservableCollection&lt;string&gt; ColumnHeaders 
          {
              get { return _columnHeaders; } 
              set
              {
                  if (_columnHeaders == value)
                      return;
  
                  _columnHeaders = value;
                  RaisePropertyChanged(() =&gt; ColumnHeaders);
              }
          }
  
          public ObservableCollection&lt;ObservableCollection&lt;object&gt;&gt; Data
          {
              get { return _data; }
              set
              {
                  if (_data == value)
                      return;
  
                  _data = value;
                  RaisePropertyChanged(() =&gt; Data);
              }
          }
  
          private ObservableCollection&lt;string&gt; _columnHeaders;
          private ObservableCollection&lt;ObservableCollection&lt;object&gt;&gt; _data;
  
          public ViewModel()
          {
              ColumnHeaders = new ObservableCollection&lt;string&gt;();
              ColumnHeaders.Add("String Column");
              ColumnHeaders.Add("Boolean Column");
  
              Data = new ObservableCollection&lt;ObservableCollection&lt;object&gt;&gt;();
  
              for(int i = 0; i &lt; 5; i++)
              {
                  ObservableCollection&lt;object&gt; row = new ObservableCollection&lt;object&gt;();
                  row.Add("cell " + i);
                  row.Add(i % 2 == 0 ? true : false);
  
                  Data.Add(row);
              }
          }
      }
  }
  </code></pre>
  
  <p>The inner collection in <code>Data</code> can contain any type of objects and there can be any number of columns.  I also need to be able to use different <code>DataTemplate</code> to render each cell depending on the object type.</p>
  
  <p>My current view xaml looks like the following:</p>
  
  <pre><code>&lt;UserControl x:Class="BindingTest.View"
               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:clr="clr-namespace:System;assembly=mscorlib"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"
               DataContext="{Binding ViewModel, Source={StaticResource Locator}}"&gt;
      &lt;UserControl.Resources&gt;
          &lt;DataTemplate x:Key="StringDataTemplate"&gt;
              &lt;TextBlock Text="{Binding}"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/UserControl.Resources&gt;
      &lt;StackPanel&gt;
          &lt;ItemsControl ItemsSource="{Binding ColumnHeaders}"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;TextBlock Width="100" Text="{Binding }"/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
          &lt;/ItemsControl&gt;
          &lt;ItemsControl ItemsSource="{Binding Data}"&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;StackPanel&gt;
                          &lt;StackPanel Orientation="Horizontal"&gt;
                              &lt;ItemsControl ItemsSource="{Binding}"&gt;
                                  &lt;ItemsControl.ItemsPanel&gt;
                                      &lt;ItemsPanelTemplate&gt;
                                          &lt;StackPanel Orientation="Horizontal"/&gt;
                                      &lt;/ItemsPanelTemplate&gt;
                                  &lt;/ItemsControl.ItemsPanel&gt;
                                  &lt;ItemsControl.ItemTemplate&gt;
                                      &lt;DataTemplate&gt;
                                          &lt;ContentPresenter Content="{Binding}"&gt;
                                              &lt;ContentPresenter.Resources&gt;
                                                  &lt;DataTemplate DataType="{x:Type clr:String}"&gt;
                                                      &lt;TextBox Width="100" Text="{Binding Path=.}"/&gt;
                                                  &lt;/DataTemplate&gt;
                                                  &lt;DataTemplate DataType="{x:Type clr:Boolean}"&gt;
                                                      &lt;CheckBox Width="100" IsChecked="{Binding Path=.}"/&gt;
                                                  &lt;/DataTemplate&gt;
                                                  &lt;DataTemplate DataType="{x:Type clr:DateTime}"&gt;
                                                      &lt;DatePicker Width="100" SelectedDate="{Binding Path=.}"/&gt;
                                                  &lt;/DataTemplate&gt;
                                              &lt;/ContentPresenter.Resources&gt;
                                          &lt;/ContentPresenter&gt;
                                      &lt;/DataTemplate&gt;
                                  &lt;/ItemsControl.ItemTemplate&gt;
                              &lt;/ItemsControl&gt;
                          &lt;/StackPanel&gt;
                      &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
          &lt;/ItemsControl&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>I would like to use a <code>GridView</code> on my <code>ListView</code> but I can't quite figure out how to bind to my data in a MVVM manner.</p>