Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF, how can I allow only a single cell to be selected in a DataGrid?

Tags:

c#

.net

wpf

I want it to only be possible to select one cell in a DataGrid. With multi select disabled, you can still select a range of cells, but I want it to only allow a single-cell selection. Is there any way to do this through the properties? Or will I have to intercept the selection and filter out everything but one cell?

like image 643
Matt Avatar asked Sep 02 '25 03:09

Matt


1 Answers

The DataGrid has a SelectionUnit property of type DataGridSelectionUnit, in conjunction with SelectionMode:

The SelectionMode and SelectionUnit properties together define the selection behavior for the DataGrid.

Try setting SelectionUnit to Cell. This will work with both SelectionMode values:

If the selection mode is Extended, the user can select multiple items where the item type is defined by the SelectionUnit property. If the selection mode is Single, the user can only select single items.

To select a single cell, use SelectionMode of Single and SelectionUnit of Cell:

The SelectionMode and SelectionUnit properties together determine how users can select items in a DataGrid. For example, if the SelectionMode is Single and the SelectionUnit is Cell, the user can select only one cell at a time in the DataGrid.

like image 107
Adam Houldsworth Avatar answered Sep 04 '25 17:09

Adam Houldsworth