Wpf DataGrid - 无法以编程方式选择单元格

时间:2013-11-10 12:38:33

标签: c# wpf datagrid datagridcolumn datagridcell

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

我从以下帖子中得到了这个:

Selecting all Cells in Column

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

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属性的值(参见附图):

enter image description here

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

enter image description here

将Cell添加到SelectedCells后,为什么Cell被置零?

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

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

0 个答案:

没有答案