1

我需要选择单击标题的列的所有单元格,

我从以下帖子中获取了这个:

选择列中的所有单元格

单击某个列时,我不想选择所有受尊重的单元格。

CS :

    private void OnColumnsClicked(object sender, RoutedEventArgs e)
    {  
        var columnHeader = (DataGridColumnHeader)e.OriginalSource;           
        this.AssociatedObject.SelectedCells.Clear();

        for (int i = 0; i < this.AssociatedObject.Items.Count; i++)
        {
            var cellInfo = new DataGridCellInfo(this.AssociatedObject.Items[i], columnHeader.Column);
            this.AssociatedObject.SelectedCells.Add(cellInfo); // Here is where the Exception is thrown when adding to the SelectedCells collection .
        }                        
    }

问题是在将第二个单元格添加到 SelectedCells 后,我得到以下异常:

 The collection already contains the item.

重要的 :

创建cellInfo时,它具有 Item 和 Column 属性的值(参见附图):

在此处输入图像描述

添加 is 到 SelectedCells 后,请参阅SelectedCells[0]

在此处输入图像描述

任何想法为什么在将 Cell 添加到 SelectedCells 后将其归零?

DataGridCellInfo (struct) 相等性检查(来自反射器):

internal bool EqualsImpl(DataGridCellInfo cell)
{
     return (((cell._column == this._column) && (cell.Owner == this.Owner)) && (cell._info == this._info));
}
4

0 回答 0