title: Is it possible to get dynamic columns on wpf datagrid in mvvm pattern?
body: <p>I'm developing a product in wpf (using the MVVM pattern). According to the user's customization (user ll select the columns) I have to display a set of data into a datagrid. Currently I'm binding an <code>ObservableCollection</code> with set of properties to the <code>ItemSource</code> of the datagrid. This limits me to fixed column size.</p>
  <p>Note: The n number of columns name is listed out for user's selection.</p>
  <p>If its done in code behind it is easy by &quot;datagrid.columns.add()&quot;. Can any one out there help me in this scenario.</p>
  <p>my xaml:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;my:DataGrid
      AutoGenerateColumns=&quot;False&quot;
      Margin=&quot;357,121.723,82,41&quot;
      Name=&quot;dataGrid3&quot;
      c:DataGridExtension.Columns=&quot;{Binding ColumnCollection}&quot;
      /&gt;
  </code></pre>
  <p>my command class:</p>
  <pre class="lang-cs prettyprint-override"><code>public static class DataGridExtension
  {
      public static ObservableCollection&lt;DataGridColumn&gt; GetColumns(DependencyObject obj)
      {
          return (ObservableCollection&lt;DataGridColumn&gt;)obj.GetValue(ColumnsProperty);
      }
  
      public static void SetColumns(
          DependencyObject obj, ObservableCollection&lt;DataGridColumn&gt; value)
      {
          obj.SetValue(ColumnsProperty, value);
      }
  
      public static readonly DependencyProperty ColumnsProperty = 
          DependencyProperty.RegisterAttached(
              &quot;Columns&quot;,
              typeof(ObservableCollection&lt;DataGridColumn&gt;),typeof(DataGridExtension),
           new UIPropertyMetadata(new ObservableCollection&lt;DataGridColumn&gt;(),
              OnDataGridColumnsPropertyChanged));
  
      private static void OnDataGridColumnsPropertyChanged(DependencyObject d,
             DependencyPropertyChangedEventArgs e)
      {
          if (d.GetType() == typeof(DataGrid))
          {
              DataGrid myGrid = d as DataGrid;
  
              var Columns = (ObservableCollection&lt;DataGridColumn&gt;)e.NewValue;
  
              if (Columns != null)
              {
                  myGrid.Columns.Clear();
  
                  if (Columns != null &amp;&amp; Columns.Count &gt; 0)
                  {
                      foreach (DataGridColumn dataGridColumn in Columns)
                      {
                          myGrid.Columns.Add(dataGridColumn);
                      }
                  }
  
                  Columns.CollectionChanged +=
                      (object sender, NotifyCollectionChangedEventArgs args)
                  {
                      if (args.NewItems != null)
                      {
                          //foreach (DataGridColumn column in args.NewItems.Cast&lt;DataGridColumn&gt;())
                          //    myGrid.Columns.Add(column);
                      }
  
                      if (args.OldItems != null)
                      {
                          //foreach (DataGridColumn column in args.OldItems.Cast&lt;DataGridColumn&gt;())
                          //    myGrid.Columns.Remove(column);
                      }
                  };
              }
          }
      }
  }
  </code></pre>
  <p>and my property in viewmodel:</p>
  <pre class="lang-cs prettyprint-override"><code>private ObservableCollection&lt;DataGridColumn&gt; _columnCollection =
      new ObservableCollection&lt;DataGridColumn&gt;();
      
  public ObservableCollection&lt;DataGridColumn&gt; ColumnCollection
  {
      get
      {
          return this._columnCollection;
      }
      set
      {
          _columnCollection = value;
          base.OnPropertyChanged(&quot;ColumnCollection&quot;);
          //Error
          //base.OnPropertyChanged&lt;ObservableCollection&lt;DataGridColumn&gt;&gt;(
          //   () =&gt; this.ColumnCollection);
      }
  }
  </code></pre>