0

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

但是,出于某种原因,我的代码表现得有些奇怪。 这就是发生的事情

这是我的代码:

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

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

4

1 回答 1

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

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

于 2013-03-08T11:08:23.270 回答