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

泪湿孤枕 提交于 2019-12-08 17:53:35

问题


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.


回答1:


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

    }
}


来源:https://stackoverflow.com/questions/4714325/wpf-datagrid-selectionchanged-event-isnt-raised-when-selectionunit-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!