2011-01-17 10 views
6

Sto utilizzando il datagrid del toolkit WPF. L'ho impostato su SelectionUnit = "Cell" e SelectionMode = "Esteso".DataGrid WPF: l'evento SelectionChanged non viene generato quando SelectionUnit = "Cell"

L'evento SelectionChanged non viene mai generato!

Funziona correttamente quando SelectionUnit è impostato su FullRow.

Mi manca qualcosa?

BTW, il motivo per cui ho bisogno è che sto cercando di creare una proprietà associata per aiutarmi a collegare le SelectedCells al mio ViewModel.

risposta

7

Fare uso di DataGrid.SelectedCellsChanged che dovrebbe essere provide you con quello che ti serve.

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(); 

    } 
} 
+0

Aaron ha ragione. SelectionChanged funziona davvero e fornisce le informazioni necessarie. – GuYsH