title: MVVM - WPF DataGrid - AutoGeneratingColumn Event
body: <p>I'm currently taking a good look at the <strong>excellent</strong> toolkit from Laurent and I have the following question.</p>
  
  <p>From Blend 4, I have added an EventTrigger for the Loaded event, in my ViewModel I have the following:</p>
  
  <pre><code>public RelayCommand rcAutoGeneratingColumn { get; private set; }
  </code></pre>
  
  <p>In the constructor I have:</p>
  
  <pre><code>rcAutoGeneratingColumn = 
     new RelayCommand(o =&gt; DataGridAutoGeneratingColumn(o));
  </code></pre>
  
  <p>Also in the ViewModel, I have the method which I wish to be invoked by the RelayCommand:</p>
  
  <pre><code>    private void DataGridAutoGeneratingColumn(Object o)
      {
          DataGrid grid = (DataGrid)o;
  
          foreach (DataGridTextColumn col in grid.Columns)
          {
              if (col.Header.ToString().ToLower() == "id")
              {
                  col.Visibility = System.Windows.Visibility.Hidden;
              }
          }
      }
  </code></pre>
  
  <p>My XAML contains the following (for the DataGrid):</p>
  
  <pre><code>&lt;i:Interaction.Triggers&gt;
      &lt;i:EventTrigger EventName="Loaded"&gt;
          &lt;GalaSoft_MvvmLight_Command:EventToCommand 
              Command="{Binding rcAutoGeneratingColumn, Mode=OneWay}"
              CommandParameter="{Binding ElementName=dataGrid1, Mode=OneWay}"/&gt;
      &lt;/i:EventTrigger&gt;
  &lt;/i:Interaction.Triggers&gt;
  </code></pre>
  
  <p><strong>There is NO PROBLEM here</strong> the code works just fine, but obviously the event used to hide certain columns should be the <strong>AutoGeneratingColumn</strong> event and not Loaded.
  I have used to Loaded event as a getaround.</p>
  
  <p>I was hoping that I could relay any event offered by the control so that, in this case, the following would work instead:</p>
  
  <pre><code>&lt;i:Interaction.Triggers&gt;
      &lt;i:EventTrigger EventName="AutoGeneratingColumn"&gt;
          &lt;GalaSoft_MvvmLight_Command:EventToCommand 
              Command="{Binding rcAutoGeneratingColumn, Mode=OneWay}"
              CommandParameter="{Binding ElementName=dataGrid1, Mode=OneWay}"/&gt;
      &lt;/i:EventTrigger&gt;
  &lt;/i:Interaction.Triggers&gt;
  </code></pre>
  
  <p>I am unable to get the AutoGeneratingColumn event to trigger, and I'm hoping that I've overlooked something and appreciate any advice given!</p>
  
  <p>This behaviour is the same with the GridControl from DevExpress, in that the Loaded event is triggered whereas the <strong>ColumnsPopulated</strong> event (this being the equivalent of the AutoGeneratingColumn event) is not.</p>
  
  <p>DevExpress offered the following information with regard to my question:</p>
  
  <p>"<em>We have reviewed this question, and come to an interesting conclusion. It looks like the visual tree is not being built at the moment when the Interaction.Triggers are being processed</em>"</p>
  
  <p>If this is true, and there is no other way in which to invoke the events within the ViewModel, then one would have to go ahead and - by using trial and error - note which of the DataGrid events (of which there are over 100) can be invoked in this way and which cannot!</p>
  
  <p>One would like to think that every event which is available in the code-behind, can also be reached when applying the MVVM pattern.</p>
  
  <p>I have searched for an answer but I cannot rule out that I have overlooked something, so if this is to be the case, then please accept my apologies!</p>