Ehud Grand
Ehud Grand

Reputation: 3693

c# wpf grid - SelectedCellsChanged not fired for cells in same row

I have a DataGrid with SelectedCellsChanged event. It is fired when selecting cells from different rows, but if I select a cell from the current row the event isn't fired.

xaml code:

  <DataGrid Name ="NavDataGrid"
                    Margin="0,0,0,0" 
                    VerticalAlignment="Top" 
                    ItemsSource="{Binding NavTable}" 
                    CanUserAddRows="False"
                    SelectionMode="Extended"        
                    AutoGenerateColumns="False"
                    SelectedCellsChanged="NavDataGrid_SelectedCellsChanged"                          
                    PreviewMouseWheel="NavDataGrid_PreviewMouseWheel">

code behind:

 private void NavDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)

I've tried the SelectionChanged event it (dosent) works the same.

I'm new to wpf so maybe it is a simple question, any idea would be great.

Thanks

Upvotes: 0

Views: 1545

Answers (1)

Mathieu
Mathieu

Reputation: 1677

Indeed, that's the way the WPF DataGrid works. You'll have to manually detect which cell has been selected.

See the MSDN doc

Selection By default, the entire row is selected when a user clicks a cell in a DataGrid, and a user can select multiple rows. You can set the SelectionMode property to specify whether a user can select cells, full rows, or both. Set the SelectionUnit property to specify whether multiple rows or cells can be selected, or only single rows or cells. You can get information about the cells that are selected from the SelectedCells property. You can get information about cells for which selection has changed in the SelectedCellsChangedEventArgs of the SelectedCellsChanged event. Call the SelectAllCells or UnselectAllCells methods to programmatically select or unselect all cells. For more information, see Default Keyboard and Mouse Behavior in the DataGrid Control.

You could hook yourself to another event (eg: MouseDown / Clicked), get the cell that has been clicked and do what you have to do after that.

I suggest giving that article a read: Detecting clicked cell and row

Upvotes: 2

Related Questions