2011-01-17 10 views
6

Tôi đang sử dụng DataFrid bộ công cụ WPF. Tôi có nó được đặt thành SelectionUnit = "Cell"SelectionMode = "Extended".WPF Datagrid: SelectionChanged sự kiện không được nâng lên khi SelectionUnit = "Cell"

Sự kiện SelectionChanged không bao giờ được nâng lên!

Nó hoạt động tốt khi SelectionUnit được đặt thành FullRow.

Tôi có thiếu gì đó không?

BTW, lý do tôi cần nó là vì tôi đang cố gắng tạo Thuộc tính được đính kèm để giúp tôi ràng buộc SelectedCells với ViewModel của mình.

Trả lời

7

Sử dụng số DataGrid.SelectedCellsChanged cần provide you với những gì bạn cần.

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

Quyền của Aaron. SelectionChanged thực sự làm việc và cung cấp các thông tin cần thiết. – GuYsH