Ahmad
Ahmad

Reputation: 1524

How to attach a Click event handler specifically on a GridViewColumnHeader

I am trying to attach a click event on a GridViewColumnHeader:

For this i used the following:

<ListView GridViewColumnHeader.Click="HandleColumnHeaderClicked">

</ListView>

and the click event is handled as follows in the code behind:

private void HandleColumnHeaderClicked(object sender, RoutedEventArgs e)
{
  if (e.OriginalSource is GridViewColumnHeader)
  {
     var column = ((GridViewColumnHeader)e.OriginalSource).Column;
     vm.SelectedColumnCommand.Execute(column.Header);
     e.Handled = true;
  }
}

All seems fine, however the GridViewColumnHeader.Click is effectively the same as ButtonBase.Click. This means that any ButtonBase controls in my list view will trigger this event and my list view could have thousands of such controls. This does not seem as the best practice.

My question is: how can I hook the click event specifically on type GridViewColumnHeader (and not on any other ButtonBase controls).

Thanks in advance.

Upvotes: 0

Views: 422

Answers (1)

dytori
dytori

Reputation: 487

Use EventSetter in Style with TargetType.

Upvotes: 1

Related Questions