I'm trying to add a DataGrid, where clicking the column header causes an action to be done on all cells below it. I can set an event handler on header click events. But since the headers are bound to string constants in the VM, their value doesn't seem to be passed along to the event handler. I can't find the value of the clicked header anywhere in the "sender" object in the event handler. Any ideas?
Here's a screenshot of the DataGrid:
XAML:
<DataGrid Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Path=InfoFieldCollection, Mode=TwoWay}"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Resources>
<Style TargetType="DataGrid">
<EventSetter Event="SelectionChanged" Handler="RovIllustrationInfoFieldDataGrid_SelectionChanged" />
</Style>
<Style TargetType="DataGridCell">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
</Style>
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="Click" Handler="RovIllustrationInfoFieldDataGridColumnHeader_Click" />
</Style>
</DataGrid.Resources>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow},
Converter={StaticResource DataGridRowToHeaderStringConverter}}" />
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DataContext.InfoFieldIsVisibleLabel, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsVisible, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
C#:
private void RovIllustrationInfoFieldDataGridColumnHeader_Click(object sender, RoutedEventArgs e) {
var header = sender as DataGridColumnHeader;
var vm = DataContext as ElementDetailsUserControlVM;
if (header != null && vm != null) {
Console.WriteLine(header.Content.ToString());
}
}