0

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. This post (WPF Mousedown => No MouseLeave Event) tells about mouse capturing, but this block

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 the 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?

Arli Chokoev
  • 521
  • 6
  • 22

1 Answers1

0

I think using ColumnHeaderDragStarted would be a good idea.

Lana
  • 1,024
  • 1
  • 7
  • 14
  • 1
    In my case `CanUserReorderColumns` set to false. My idea is to select columns with a single mouse movement with left button pressed. It wouldn't work with `CanUserReorderColumns` set to true, because in this case the column gets moved instead of selection. – Arli Chokoev Jun 29 '20 at 06:33