title: Wpf ItemsControl.ItemsPanelTemplate as a Grid and add new items in different columns
body: <p>I'm working on a dynamicaly created grid containing in each new column a new item added to an ItemsControl.</p>
  
  <p>I'm using Rachel Lim's <a href="https://stackoverflow.com/a/9007442/1462911">GridHelper</a></p>
  
  <p>For now, i have a main window like following,</p>
  
  <p>Xaml code:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:WpfApplication1"
      Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;local:StarsConverter x:Key="conv"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="*"/&gt;
              &lt;ColumnDefinition Width="9*"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Button Grid.Column="0" Command="{Binding Add}"/&gt;
          &lt;ItemsControl x:Name="list" Grid.Column="1" ItemsSource="{Binding Oc}"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;Grid x:Name="grid" Background="Black"
                        local:GridHelpers.StarColumns="{Binding ColumnCount, Converter={StaticResource conv}, Mode=OneWay}" local:GridHelpers.ColumnCount="{Binding ColumnCount}"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
              &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Canvas Loaded="canvas_Loaded" x:Name="canvas" Background="White"&gt;
                          &lt;Border  Canvas.Left="25" Canvas.Top="25" Height="25" Width="50" Background="Red"&gt;
                              &lt;TextBlock Text="{Binding}"/&gt;
                          &lt;/Border&gt;
                      &lt;/Canvas&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.ItemTemplate&gt;
          &lt;/ItemsControl&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><br/>
  and Xaml.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>public partial class MainWindow : Window
  {
      private Canvas canvas;
      public MainWindow()
      {
          InitializeComponent();
  
          this.DataContext = new ViewModel();
      }
  
      private void canvas_Loaded(object sender, RoutedEventArgs e)
      {
          ViewModel vm = this.DataContext as ViewModel;
          canvas = sender as Canvas;
          Binding b = new Binding();
          b.Source = vm;
          b.Path = new PropertyPath(ViewModel.ColumnCountProperty);
          b.Mode = BindingMode.OneWay;
          canvas.SetBinding(Grid.ColumnProperty, b);
      }
  }
  </code></pre>
  
  <p>This MainWindow has a ViewModel as DataContext:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ViewModel : DependencyObject
  {
      private RelayCommand add;
  
      public ViewModel()
      {
      }
  
      public ObservableCollection&lt;String&gt; Oc
      {
          get { return (ObservableCollection&lt;String&gt;)GetValue(OcProperty); }
          set { SetValue(OcProperty, value); }
      }
  
      // Using a DependencyProperty as the backing store for Oc.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty OcProperty =
          DependencyProperty.Register("Oc", typeof(ObservableCollection&lt;String&gt;), typeof(ViewModel), new UIPropertyMetadata(new ObservableCollection&lt;string&gt;()));
  
      public int ColumnCount
      {
          get { return (int)GetValue(ColumnCountProperty); }
          set { SetValue(ColumnCountProperty, value); }
      }
  
      // Using a DependencyProperty as the backing store for ColumnCount.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty ColumnCountProperty =
          DependencyProperty.Register("ColumnCount", typeof(int), typeof(ViewModel), new UIPropertyMetadata(0));
  
      public RelayCommand Add
      {
          get
          {
              if (this.add == null)
                  this.add = new RelayCommand(param =&gt; this.AddString(), param =&gt; this.CanAddString());
              return this.add;
          }
      }
  
      private bool CanAddString()
      {
          return true;
      }
  
      private void AddString()
      {
          this.Oc.Add("test" + ColumnCount);
          ColumnCount++;
      }
  }
  </code></pre>
  
  <p>When i click the button, the command is doing fine, so i have a new itemscontrol item and my grid as ItemsPanelTemplate is updating with new ColumnDefinition but the item is not in the right column.</p>