我试图通过连接到数据网格的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);
}
}有人能告诉我我错过了什么吗?
发布于 2013-03-08 19:08:23
好吧,我摆脱了这个行为,但偶然发现了新代码的另一个问题,看起来是这样的:
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);
}
}现在我不能将我的选择扩展到右侧的列(这很好),但我仍然可以扩展到左侧。
https://stackoverflow.com/questions/15292315
复制相似问题