WPF数据表格:当SelectionUnit="Cell"时,SelectionChanged事件不会被触发。

12
我正在使用WPF工具包中的数据网格。我将其设置为 SelectionUnit="Cell"SelectionMode="Extended"
但是,SelectionChanged事件从未被触发!
当SelectionUnit设置为FullRow时,它可以正常工作。
我错过了什么吗?
顺便说一下,我需要这样做的原因是因为我正在尝试创建一个附加属性来帮助我将SelectedCells绑定到我的ViewModel。
1个回答

13

使用DataGrid.SelectedCellsChanged,它应该为您提供所需的内容。

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

    }
}

亚伦是正确的。SelectionChanged确实起作用并提供所需的信息。 - GuYsH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接