title: WPF Dynamic Layout with ItemsControl and Grid
body: <p>I am creating a WPF form. One of the requirements is that it have a sector-based layout so that a control can be explicitly placed in one of the sectors/cells.</p>
  
  <p>I have created a tic-tac-toe example below to convey my problem:</p>
  
  <p>There are two types and one base type:</p>
  
  <pre><code>public class XMoveViewModel : MoveViewModel
  {
  }
  public class OMoveViewModel : MoveViewModel
  {
  }
  public class MoveViewModel
  {
      public int Row { get; set; }
      public int Column { get; set; }
  }
  </code></pre>
  
  <p>The DataContext of the form is set to an instance of:</p>
  
  <pre><code>public class MainViewModel : ViewModelBase
  {
      public MainViewModel()
      {
          Moves = new ObservableCollection&lt;MoveViewModel&gt;()
          {
              new XMoveViewModel() { Row = 0, Column = 0 },
              new OMoveViewModel() { Row = 1, Column = 0 },
              new XMoveViewModel() { Row = 1, Column = 1 },
              new OMoveViewModel() { Row = 0, Column = 2 },
              new XMoveViewModel() { Row = 2, Column = 2}
          };
      }
      public ObservableCollection&lt;MoveViewModel&gt; Moves
      {
          get;
          set;
      }
  }
  </code></pre>
  
  <p>And finally, the XAML looks like this:</p>
  
  <pre><code> &lt;Window.Resources&gt;
      &lt;DataTemplate DataType="{x:Type vm:XMoveViewModel}"&gt;
          &lt;Image Source="XMove.png" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Column}" Stretch="None" /&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type vm:OMoveViewModel}"&gt;
          &lt;Image Source="OMove.png" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Column}" Stretch="None" /&gt;
      &lt;/DataTemplate&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;ItemsControl ItemsSource="{Binding Path=Moves}"&gt;
          &lt;ItemsControl.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;Grid ShowGridLines="True"&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition /&gt;
                          &lt;RowDefinition /&gt;
                          &lt;RowDefinition /&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition /&gt;
                          &lt;ColumnDefinition /&gt;
                          &lt;ColumnDefinition /&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                  &lt;/Grid&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ItemsControl.ItemsPanel&gt;
      &lt;/ItemsControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>What was not so obvious to me when I started was that the ItemsControl element actually wraps each item in a container, so my Grid.Row and Grid.Column bindings are ignored since the images are not directly contained within the grid. Thus, all of the images are placed in the default Row and Column (0, 0).</p>
  
  <p>What is happening:</p>
  
  <p><a href="https://i.stack.imgur.com/yMvkv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yMvkv.png" alt="What is happening"></a>
  </p>
  
  <p>The desired result:</p>
  
  <p><a href="https://i.stack.imgur.com/MeHee.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MeHee.png" alt="The desired result"></a>
  </p>
  
  <p>So, my question is this: how can I achieve the dynamic placement of my controls in a grid? I would prefer a XAML/Data Binding/MVVM-friendly solution.</p>
  
  <p>Thanks.</p>
  
  <p>I have put the final code here:
  <a href="http://www.centrolutions.com/downloads/TicTacToe.zip" rel="nofollow noreferrer">http://www.centrolutions.com/downloads/TicTacToe.zip</a></p>