I have following events with subscriptions in the class, derived from DataGrid:
private void SubscribeHeaders(DependencyObject sender = null)
{
sender = sender ?? this;
for (int childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(sender); childIndex++)
{
DependencyObject child = VisualTreeHelper.GetChild(sender, childIndex);
if (child is DataGridColumnHeader colHeader)
{
//Subscribe column headers
colHeader.PreviewMouseLeftButtonDown += (obj, e) => OnColumnHeaderMouseDown(obj, e);
colHeader.PreviewMouseLeftButtonUp += (obj, e) => OnColumnHeaderMouseUp(obj, e);
colHeader.MouseEnter += (obj, e) => OnColumnHeaderMouseEnter(obj, e);
colHeader.MouseLeave += (obj, e) => OnColumnHeaderMouseLeave(obj, e);
}
else if (child is DataGridRowHeader rowHeader)
{
//Subscribe row headers
}
else
{
SubscribeHeaders(child);
}
}
}
Everything binds fine, but OnColumnHeaderMouseEnter(obj, e) is not executed, while mouse button is pressed. It doesn't seem like mouse is captured,
if (Mouse.Captured != null)
Mouse.Captured.ReleaseMouseCapture();
does not change anything, since Mouse.Captured is always null.
What I'm trying to achieve is to select a range of DataGrid columns by dragging a mouse with a pressed left button from start to the end of selection. My idea is to react on mouse buttons and movements from header to header.
How can I capture movements between DataGridColumnHeaders with a mouse button pressed?