WPF

WPF DataGrid row double-click: the command that fires on headers too

S. Iguchi WPF Notes s-iguchi
WPF

The usual way to wire a WPF DataGrid row double-click to a command also fires when a column header or scroll bar is double-clicked. The article measures four approaches with real mouse input and singles out a row-level attached property.

Key points

  • EventTrigger on the DataGrid passes SelectedItem, so a header double-click opens the row selected before.
  • MouseBinding stays silent when the second MouseLeftButtonDown is already handled, as on editable cells and Ctrl clicks.
  • Control's class handler registers with handledEventsToo, so MouseDoubleClick still fires on headers and scroll bars.
  • On an editable cell the first click starts editing and the display TextBlock's visual parent is already null.
  • A RowStyle attached property delivered the correct row in every measured case, including editable cells.
  • Right-button double-clicks raise the same event, so the handler has to check for the left button.

Why it matters

This bug quietly appears in every WPF project that wires MVVM through Microsoft.Xaml.Behaviors.Wpf, not just in one grid: a user sorting by clicking a header opens the details screen of a row that is not even visible, and rarely reports it as a bug. Moving to a row-level attached property looks like a small change, but the command parameter then comes from the row's DataContext instead of SelectedItem, and multi-select behaviour with Ctrl has to be reconsidered. Before adopting it, verify the package version (1.1.135) and targets other than .NET 10 in your own environment; the author did not measure .NET Framework 4.8.

Real-world use

  • Opening a details dialog from a line-of-business DataGrid such as a customer list
  • Keeping a wrong record from opening while the user sorts by clicking a column header
  • Stopping the command from running when the blank area below a short list is double-clicked
  • Letting edit mode start on an editable cell without breaking the row double-click

This summary was generated by the agent from the original post on S. Iguchi WPF Notes. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message