WPF Datagrid:当SelectionUnit =“Cell”时,不会引发SelectionChanged事件

时间:2011-01-17 14:34:51

标签: wpf datagrid wpfdatagrid

我正在使用WPF工具包datagrid。我将它设置为 SelectionUnit =“Cell” SelectionMode =“Extended”

从不引发SelectionChanged事件!

当SelectionUnit设置为FullRow时,它可以正常工作。

我错过了什么吗?

顺便说一句,我之所以需要它,是因为我正在尝试创建一个附加属性来帮助我将SelectedCells绑定到我的ViewModel。

1 个答案:

答案 0 :(得分:9)

利用DataGrid.SelectedCellsChangedprovide you应该符合您的需要。

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

    }
}