Home WPF DataGrid - How detect if full row is selected?
Reply: 2

WPF DataGrid - How detect if full row is selected?

google fan
google fan Published in 2017-12-07 20:08:32Z

I have DataGrid in my WPF application, and I want to run a function in the SelectedCellsChanged event, but only if the row is not fully selected, which happens if the user clicks at the header of the row.

My table has only one column.

I've tried the following code and always the value of rowContainer.IsSelected is false.

How can I do this?

Private Sub DataGridEx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles Me.SelectionChanged

    If SelectedCells.Count <> 1 Then
        Exit Sub
    End If

    If Not SelectedCells.First.Item.ToString.Contains("NewItemPlaceholder") Then

                    Dim cell = SelectedCells.First
                    Dim index = Items.IndexOf(cell.Item) + 1
                    Dim rowContainer As DataGridRow = ItemContainerGenerator.ContainerFromIndex(index)

                    If rowContainer IsNot Nothing Then
                        If Not rowContainer.IsSelected Then
                            ' run a function here
                        End If
                    End If
                End Sub,

     End If

End Sub
mm8 Reply to 2017-12-08 15:56:57Z

Get the row container for the select cell and check its IsSelected property:

private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    DataGrid dg = sender as DataGrid;
    if (dg.SelectedCells != null && dg.SelectedCells.Count > 0)
        var cell = dg.SelectedCells[0];
        var row = dg.ItemContainerGenerator.ContainerFromItem(cell.Item) as DataGridRow;
        if (row != null && row.IsSelected)
            MessageBox.Show("row is selected...");

This should work if you are using a SelectionUnit of CellOrRowHeader.

DRapp Reply to 2017-12-07 21:57:09Z

One thing you can do is force your datagrid to the SelectionUnit as FullRow, Cell or CellOrRowHeader

dim dg = new DataGrid();
dg.SelectionUnit == System.Windows.Controls.DataGridSelectionUnit.FullRow;
dg.SelectionUnit == System.Windows.Controls.DataGridSelectionUnit.CellOrRowHeader;
dg.SelectionUnit == System.Windows.Controls.DataGridSelectionUnit.Cell;

In XAML, you would just apply the property setting

<DataGrid SelectionUnit="FullRow" ... >

Then you should be able to simply hook into the SelectedCellsChanged or SelectionChanged method to act on it...

Then you don't have to worry about a partial row selection of just a cell or two and not trigger an entire row you are expecting to process. By forcing a full row, it will have in its collection one entry per row. Otherwise, if you have it by cell and have 5 columns wide and person selects 3 cells on two rows, it will have 6 cells representing the same row for each individual cell covering the rows..


You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323505 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO