WPF DataGrid column header mouse events

  • Thread starter Thread starter Arli Chokoev
  • Start date Start date
A

Arli Chokoev

Guest
I'm trying to extend standard `DataGrid` functionality by adding some methods and properties into a derived control:

public class ExtendedGrid : DataGrid
{
...
}
But handling mouse events of the headers is still unclear to me. Since DataGridColumnHeader is not a part of the visual tree (correct me if I mistake), and the only way to set an event handler to the `MouseEvent` is to apply style on it (How do I capture "Click" events on a DataGrid column headers).

Since I'm extending the DataGrid, I'd like to keep implementation in code, without adding any XAML, in my opiinion it's inconsistent in terms of code readability. Therefore, I wrote the following code:


private void InitializeStyles()
{
Style headerStyle = new Style(typeof(DataGridColumnHeader));
headerStyle.Setters.Add(new EventSetter(MouseDownEvent, new MouseButtonEventHandler(OnColumnHeaderMouseDown)));
foreach(var column in Columns)
{
column.HeaderStyle = headerStyle;
}
}

This code is called on AutoGeneratedColumns and doesn't work well (e.g. at all). Even if it would, it would impose limitations on setting styles on headers.

Is there a way to handle DataGridColumnHeader mouse events in my ExtendedGrid without any XAML? Or XAML styles is anyway better than search of workarounds (and probably overcomplicationg things)?



Continue reading...
 
Back
Top