WPF DataGrid:禁用多列选择

时间:2013-03-08 10:54:06

标签: wpf datagrid wpfdatagrid

我试图阻止用户通过挂钩到DataGrid的SelectedCellsChanged事件来对多个列进行单元格选择。

出于某种原因,我的代码表现得有些奇怪。 This is what happens

这是我的代码:

private void chartDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    foreach (DataGridCellInfo cell in e.AddedCells)
    {
        // If a selected cell is within a different column than the first selected cell, undo the selection (to prevent selections from crossing multiple columns)
        if (cell.Column != e.AddedCells[0].Column)
                    this.chartDataGrid.SelectedCells.Remove(cell);
    }
}

谁能告诉我这里缺少什么?

1 个答案:

答案 0 :(得分:0)

好的,我摆脱了这种行为,但偶然发现了新代码的另一个问题,如下所示:

private void chartDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    foreach (DataGridCellInfo cell in e.AddedCells)
    {
        // If a selected cell is within a different column than the first selected cell, undo the selection (to prevent selections from crossing multiple columns)
        if (cell.Column != this.chartDataGrid.SelectedCell[0].Column)
            this.chartDataGrid.SelectedCells.Remove(cell);
    }
}

现在我无法将我的选择扩展到右侧的列(这很好),但我仍然可以在左侧。