title: Binding to DataGridColumnHeader.Command
body: <p>I need to use <code>Command</code> on <code>DataGridColumnHeader</code> to sort my data. Standard <code>DataGrid</code> sorting feature is not enough for me because there are not all data displayed in <code>DataGrid</code>. I don't even have all data present in my VM. (It's not possible... it's too much) I just request concrete page of data from server. And now I would also like to get concrete page from sorted data.</p>
  
  <p>So I did this:</p>
  
  <pre class="lang-xml prettyprint-override"><code> &lt;DataGrid ItemsSource="{Binding Path=Entities, Mode=OneWay}" CanUserSortColumns="False" SelectionMode="Single" SelectedItem="{Binding Path=SelectedEntity}"&gt;
        &lt;DataGrid.ColumnHeaderStyle&gt;
             &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
                 &lt;Setter Property="Command" Value="{Binding Path=MyCommand}"/&gt;
                 &lt;Setter Property="CommandParameter" Value="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/&gt;
             &lt;/Style&gt;
        &lt;/DataGrid.ColumnHeaderStyle&gt;
        &lt;DataGrid.Columns&gt;
             &lt;DataGridTemplateColumn &gt;
                  &lt;DataGridTemplateColumn.Header&gt;
                       &lt;DataGridColumnHeader Content="Column1" /&gt;
                   &lt;/DataGridTemplateColumn.Header&gt;
                   &lt;DataGridTemplateColumn.CellTemplate&gt;
                        &lt;DataTemplate&gt;
                            &lt;TextBox Text="{Binding Path=Property1}"/&gt;
                        &lt;/DataTemplate&gt;
                   &lt;/DataGridTemplateColumn.CellTemplate&gt;
             &lt;/DataGridTemplateColumn&gt;
        &lt;/DataGrid.Columns&gt;
   &lt;/DataGrid&gt;
  </code></pre>
  
  <p>It works great. And now I just want to have <code>AutoGenerateColumns="True"</code> unfortunately it works no more. Is there anybody who can <strong>explain</strong> why it doesn't work for autogenerated columns and provide me with some solution? Thank you in advance!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>It has probably something to do with the fact that following doesn't work either. </p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding Path=Entities, Mode=OneWay}" CanUserSortColumns="False" SelectionMode="Single" SelectedItem="{Binding Path=SelectedEntity}"&gt;
    &lt;DataGrid.ColumnHeaderStyle&gt;
         &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
             &lt;Setter Property="Command" Value="{Binding Path=MyCommand}"/&gt;
             &lt;Setter Property="CommandParameter" Value="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/&gt;
         &lt;/Style&gt;
    &lt;/DataGrid.ColumnHeaderStyle&gt;
    &lt;DataGrid.Columns&gt;
         &lt;DataGridTemplateColumn&gt;
              &lt;!-- HERE IS THE CHANGE --&gt;
              &lt;DataGridTemplateColumn.Header&gt;Column1&lt;/DataGridTemplateColumn.Header&gt;
               &lt;DataGridTemplateColumn.CellTemplate&gt;
                    &lt;DataTemplate&gt;
                        &lt;TextBox Text="{Binding Path=Property1}"/&gt;
                    &lt;/DataTemplate&gt;
               &lt;/DataGridTemplateColumn.CellTemplate&gt;
         &lt;/DataGridTemplateColumn&gt;
    &lt;/DataGrid.Columns&gt;
  </code></pre>
  
  <p></p>
  
  <p>But style is applied. I know this because I tried:</p>
  
  <pre><code>&lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
     &lt;Setter Property="Background" Value="Tomato"/&gt; &lt;!-- beautiful tomato background --&gt;
     &lt;Setter Property="Command" Value="{Binding Path=MyCommand}"/&gt;
     &lt;Setter Property="CommandParameter" Value="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Column header has Tomato background, but command doesn't work.</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>Following is the solution. Apparently <code>DataGridColumnHeaders</code> doesn't inherit DataContext, because when I change the command binding everything works again.</p>
  
  <pre><code>&lt;Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                  Path=DataContext.MyCommand}"/&gt;
  </code></pre>
  
  <p>But still I don't quite understand why. So answer with detailed explanation will get the bounty. </p>