Wpf DataGrid - Unable to select cells programmatically

两盒软妹~` 提交于 2020-01-05 04:03:58

问题


I need to select all the cells of a column which header was clicked ,

i took this from the following post :

Selecting all Cells in Column

when a certain column is clicked i wan't to select all it's respected cells.

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 .
        }                        
    }

the problem is that after adding the second cell to SelectedCells i get the following exception :

 The collection already contains the item.

Important :

When creating cellInfo it has a value for Item and Column properties (See attached image) :

After adding is to SelectedCells , see SelectedCells[0] :

Any ideas why the Cell is being zero'd after adding it to SelectedCells ?

DataGridCellInfo (struct) equality check (from reflector):

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

来源:https://stackoverflow.com/questions/19889780/wpf-datagrid-unable-to-select-cells-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!