Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid: SelectionChanged event isn't raised when SelectionUnit="Cell"

I'm using the WPF toolkit datagrid. I have it set to SelectionUnit="Cell" and SelectionMode="Extended".

The SelectionChanged event is never raised!

It works fine when the SelectionUnit is set to FullRow.

Am I Missing something?

BTW, the reason I need it is since I'm trying to create an Attached Property to help me bind the SelectedCells to my ViewModel.

like image 552
GuYsH Avatar asked Jan 17 '11 14:01

GuYsH


1 Answers

Make use of DataGrid.SelectedCellsChanged which should provide you with what you need.

private void DG1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    //Get the newly selected cells
    IList<DataGridCellInfo> selectedcells = e.AddedCells;

    //Get the value of each newly selected cell
    foreach (DataGridCellInfo di in selectedcells)
    {
        //Cast the DataGridCellInfo.Item to the source object type
        //In this case the ItemsSource is a DataTable and individual items are DataRows
        DataRowView dvr = (DataRowView)di.Item;

        //Clear values for all newly selected cells
        AdventureWorksLT2008DataSet.CustomerRow cr = (AdventureWorksLT2008DataSet.CustomerRow)dvr.Row;
        cr.BeginEdit();
        cr.SetField(di.Column.DisplayIndex, "");
        cr.EndEdit();

    }
}
like image 76
Aaron McIver Avatar answered Sep 19 '22 12:09

Aaron McIver