C# Wpf DataGrid-无法以编程方式选择单元格
我需要选择单击标题的列的所有单元格 我是从下面的帖子中得到的: 当单击某个列时,我不想选择它的所有相关单元格 政务司司长: 重要: 创建cellInfo时,它具有项和列属性的值(请参见附图): 将is添加到SelectedCells后,请参见SelectedCells[0]: 你知道为什么将单元格添加到SelectedCells后会置零吗 DataGridCellInfo(结构)相等性检查(来自反射器):C# Wpf DataGrid-无法以编程方式选择单元格,c#,wpf,datagrid,datagridcolumn,datagridcell,C#,Wpf,Datagrid,Datagridcolumn,Datagridcell,我需要选择单击标题的列的所有单元格 我是从下面的帖子中得到的: 当单击某个列时,我不想选择它的所有相关单元格 政务司司长: 重要: 创建cellInfo时,它具有项和列属性的值(请参见附图): 将is添加到SelectedCells后,请参见SelectedCells[0]: 你知道为什么将单元格添加到SelectedCells后会置零吗 DataGridCellInfo(结构)相等性检查(来自反射器): 我找到了一个为我选择细胞的答案:ouflak的答案 private void
我找到了一个为我选择细胞的答案:ouflak的答案
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 collection already contains the item.
internal bool EqualsImpl(DataGridCellInfo cell)
{
return (((cell._column == this._column) && (cell.Owner == this.Owner)) && (cell._info == this._info));
}