The WPF DataGrid is a very flexible tool, however in its current state certain simple tasks can prove to be rather tricky. A fairly common task when working with DataGrid is detecting which row, or cell a user has clicked on, or whether they clicked a column header. You might expect this information to be readily available in the form of events, after all, the Windows Forms DataGridView has CellClicked and ColumnHeaderMouseClick events (among many others). However, sadly this is not the case. In order to implement this behaviour you have to understand the visual tree of the DataGrid and how it can be navigated.
Let's say for example you wanted to find the DataGrid item (cell, row, header) that was clicked when the right mouse button is released. Firstly, we add an event handler for the mouse click in our code-behind:
The above event is a 'bubbling' event, which means that it it started on the element that was originally clicked (for example a TextBlock which renders the cell's value within a DataGridCell), then bubbled up the logical tree until it reaches our event handler in the Window. The e.OriginalSource property gives us access to the element that initiated this event.
The problem is that while we have access to the lement which was clicked on, this element is part of the control or data template of the element that we are really interested, the cell or header. The WPF rich-content model means that our cells could contain all sorts of visual element, therefore we have no way of guessing exactly what e.OriginalSource will be. However, the one thing of which we can be certain is that this element is a child of the element which we are interested in.
If you place a bearkpoint within yoru event handler, you can then use the excellent Mole debug visualiser to locate the clicked element within the visual tree as illustrated below:
As you can see, the visual tree is a complex beast! I have highlighted the items of interest:
- The TextBlock, which is the element I clicked on, which is e.OriginalSource parameter.
- The DataGridCell, the cell which was clicked on.
- The DataGridRow which the cell belongs to.
- And finally, the DataGrid.
Therefore, in order to locate the cell and row that was clicked on we must navigate up the Visual Tree, searching by type:
Fantastic, we have now have our header and cell. All that's left to do is extract the cell's row and column indices, and cell value. Wait a minute ... where are the cell.RowIndex and cell.ColumnIndex properties? It looks like there's more work to be done.
Once we have navigated up the tree to the DataGridCell, we can continue our journey upwards to obtain the DataGridRow:
Does the DataGridRow have a RowIndex property? I think you can guess the answer to that question.
The DataGrid is an ItemsControl - WPF users are probably most familiar with the ListView which is also an ItemsControl whcih has a number of similarities with the DataGrid. In the ItemsControl terminology, the DataGridRow is an ItemContainer and the DataGrid has an ItemContainerGenerator associated with it for generating the rows. I don't want to go into the details of how ItemContainers work, Dr. WPF has a good series on the ItemsControl for those who are interested. The following code can be used to determine the index of a row:
Now that we have the row index, the column index is thankfully a little easier to locate, cell.Column.DisplayIndex does the trick. The final piece of information which we might like is the cell value. Is there a cell.Value properly? don't make me laugh!
The following method determines the property binding for the cells column, then extracts the value from the data items associated with the row:
Putting it all together, this blog post has a small smaple application which displays the header index and value, or cell's row/column indices and value in response to a right mouse click:
The sample project can be download, wpfdatagridmouseclicks, changing the file extension from .doc to .zip.
Regards, Colin E.