protected internal override void PrepareContainer(DataGridContext dataGridContext, object item) { base.PrepareContainer(dataGridContext, item); if (dataGridContext.SelectedCellsStore.Contains(DataGridVirtualizingPanel.GetItemIndex(this.ParentRow), this.ParentColumn.VisiblePosition)) { this.SetIsSelected(true); } }
public void UpdateSelectedCellsInChangeOfDataGridContext() { var removedCellsRangeWithItems = new List <SelectionCellRangeWithItems>(); var unselectedCellsFromRemove = this.GetUnselectedCellsFromRemove(removedCellsRangeWithItems); var ownerSelectedCells = m_owner.SelectedCellsStore; for (int i = 0; i < m_sourceChanges.Count; i++) { var sourceChangeInfo = m_sourceChanges[i]; switch (sourceChangeInfo.Action) { case NotifyCollectionChangedAction.Add: { if (sourceChangeInfo.StartIndex != -1) { ownerSelectedCells.OffsetIndex(sourceChangeInfo.StartIndex, sourceChangeInfo.Count); } break; } case NotifyCollectionChangedAction.Remove: break; default: throw new NotSupportedException("Only Add and Remove are supported."); } } foreach (var cellRangeWithItems in removedCellsRangeWithItems) { ownerSelectedCells.Remove(cellRangeWithItems); ownerSelectedCells.OffsetIndex(cellRangeWithItems.ItemRange.StartIndex, -cellRangeWithItems.ItemRange.Length); } if ((m_cellsToUnselect.Count > 0) || (m_cellsToSelect.Count > 0) || (unselectedCellsFromRemove.Any())) { var realizedDataRows = new Dictionary <int, DataRow>(); // Only want to update the realizedCells if the selection change is from user interaction and not // from the source being updated. When the source is updated, the generator will recreate needed container, // so the old one will have the correct selection state. foreach (var container in m_owner.CustomItemContainerGenerator.RealizedContainers) { var dataRow = container as DataRow; if ((dataRow != null) && (DataGridControl.GetDataGridContext(dataRow) == m_owner)) { realizedDataRows[DataGridVirtualizingPanel.GetItemIndex(dataRow)] = dataRow; } } // We use the ColumnsByVisiblePosition for when column are changing position, we want to have the state before the change. var columnsByVisiblePositionLikedList = m_owner.ColumnsByVisiblePosition; var columnIndex = 0; var columnsVisiblePosition = new Dictionary <ColumnBase, int>(columnsByVisiblePositionLikedList.Count); var columnNode = columnsByVisiblePositionLikedList.First; while (columnNode != null) { columnsVisiblePosition.Add(columnNode.Value, columnIndex); columnIndex++; columnNode = columnNode.Next; } foreach (var cellRangeWithItems in m_cellsToUnselect) { this.SetIsSelectedOnDataCell(columnsVisiblePosition, realizedDataRows, cellRangeWithItems, false); ownerSelectedCells.Remove(cellRangeWithItems); } foreach (var cellRangeWithItems in m_cellsToSelect) { this.SetIsSelectedOnDataCell(columnsVisiblePosition, realizedDataRows, cellRangeWithItems, true); ownerSelectedCells.Add(cellRangeWithItems); } foreach (var cellRangeWithItems in unselectedCellsFromRemove) { m_cellsToUnselect.Add(cellRangeWithItems); } } }
public void UpdateSelectedItemsInChangeOfDataGridContext() { var removedRangeWithItems = new List <SelectionRangeWithItems>(); var unselectedItemsFromRemove = this.GetUnselectedItemsFromRemove(removedRangeWithItems); var ownerSelectedItems = m_owner.SelectedItemsStore; for (int i = 0; i < m_sourceChanges.Count; i++) { var sourceChangeInfo = m_sourceChanges[i]; switch (sourceChangeInfo.Action) { case NotifyCollectionChangedAction.Add: { if (sourceChangeInfo.StartIndex != -1) { ownerSelectedItems.OffsetIndex(sourceChangeInfo.StartIndex, sourceChangeInfo.Count); } break; } case NotifyCollectionChangedAction.Remove: break; default: throw new NotSupportedException("Only Add and Remove are supported."); } } foreach (SelectionRangeWithItems rangeWithItems in removedRangeWithItems) { ownerSelectedItems.Remove(rangeWithItems); ownerSelectedItems.OffsetIndex(rangeWithItems.Range.StartIndex, -rangeWithItems.Length); } for (int i = m_toDeferSelect.Count - 1; i >= 0; i--) { object item = m_toDeferSelect[i]; int itemIndex = m_owner.Items.IndexOf(item); if (itemIndex >= 0) { if (!m_itemsToUnselect.Contains(itemIndex)) { m_itemsToSelect.Add(new SelectionRangeWithItems(itemIndex, item)); } m_toDeferSelect.RemoveAt(i); } } if ((m_itemsToUnselect.Count > 0) || (m_itemsToSelect.Count > 0) || (unselectedItemsFromRemove.Any())) { var realizedDataRows = new Dictionary <int, DataRow>(); // Only want to update the realizedDataRows if the selection change is from user interaction and not // from the source being updated. When the source is updated, the generator will recreate needed container, // so the old one will have the correct selection state. foreach (var container in m_owner.CustomItemContainerGenerator.RealizedContainers) { var dataRow = container as DataRow; if ((dataRow != null) && (DataGridControl.GetDataGridContext(dataRow) == m_owner)) { realizedDataRows[DataGridVirtualizingPanel.GetItemIndex(dataRow)] = dataRow; } } var sourceItemIsDataRow = false; foreach (var rangeWithItems in m_itemsToUnselect) { sourceItemIsDataRow |= this.SetIsSelectedOnDataRow(realizedDataRows, rangeWithItems, false); ownerSelectedItems.Remove(rangeWithItems); } foreach (var rangeWithItems in m_itemsToSelect) { sourceItemIsDataRow |= this.SetIsSelectedOnDataRow(realizedDataRows, rangeWithItems, true); ownerSelectedItems.Add(rangeWithItems); } foreach (var rangeWithItems in unselectedItemsFromRemove) { m_itemsToUnselect.Add(rangeWithItems); } if (!sourceItemIsDataRow) { foreach (var realizedItemPair in realizedDataRows) { if (ownerSelectedItems.Contains(new SelectionRange(realizedItemPair.Key))) { realizedItemPair.Value.SetIsSelected(true); } else { realizedItemPair.Value.SetIsSelected(false); } } } } }