Note that there are some explanatory texts on larger screens.

plurals
  1. PODataGrid.ColumnHeaderStyle and Command Binding
    text
    copied!<p>In the XAML below, the command property is correctly working only with Column B. Clicking on Column A's header is not invoking command's execute method. The difference is that in Column B, DataGridColumnHeader is instantiated explicitly.</p> <p>On other hand, the second part of style, the trigger that sets background base on Pressed state is working for both columns.</p> <p>Why does the Command property work with Column B &amp; not with Column A ?</p> <pre><code>&lt;DataGrid x:Name="Test" ItemsSource="{Binding Items}" AutoGenerateColumns="False" &gt; &lt;DataGrid.ColumnHeaderStyle&gt; &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt; &lt;Setter Property="Command" Value="{Binding MyCommand}"/&gt; &lt;Setter Property="CommandParameter" Value="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsPressed" Value="True"&gt; &lt;Setter Property="Background" Value="Red" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/DataGrid.ColumnHeaderStyle&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn FontSize="12" Header="Column A" Width="200" Binding="{Binding AData}" /&gt; &lt;DataGridTextColumn FontSize="12" Width="200" Binding="{Binding BData}"&gt; &lt;DataGridTextColumn.Header&gt; &lt;DataGridColumnHeader Content="Column B" /&gt; &lt;/DataGridTextColumn.Header &gt; &lt;/DataGridTextColumn&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p><strong>EDIT</strong></p> <p>Data context:</p> <pre><code>namespace TestColumnHeaderCommandBinding { public class Item { public int AData { get; set; } public string BData { get; set; } } public class MyCommand : ICommand { #region ICommand Members public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { MessageBox.Show(parameter.ToString() + " clicked"); } #endregion } public class DataContext { public DataContext() { MyCommand = new TestColumnHeaderCommandBinding.MyCommand(); Items = new List&lt;Item&gt;(5); Items.Add(new Item { AData = 1, BData = "One" }); Items.Add(new Item { AData = 2, BData = "Two" }); Items.Add(new Item { AData = 3, BData = "Three" }); Items.Add(new Item { AData = 4, BData = "Four" }); Items.Add(new Item { AData = 5, BData = "Five" }); } public MyCommand MyCommand { get; set; } public List&lt;Item&gt; Items { get; set; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload