public void Visit( DataGridContext sourceContext, ref bool stopVisit ) { object[] items; CollectionView itemsCollection = sourceContext.Items; int count = itemsCollection.Count; if( count == 0 ) return; if( sourceContext.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase ) { items = null; } else { items = new object[ count ]; for( int i = 0; i < count; i++ ) { items[ i ] = itemsCollection.GetItemAt( i ); } } SelectionRange itemRange = new SelectionRange( 0, count - 1 ); if( sourceContext.DataGridControl.SelectionUnit == SelectionUnit.Row ) { sourceContext.DataGridControl.SelectionChangerManager.SelectItems( sourceContext, new SelectionRangeWithItems( itemRange, items ) ); } else { HashedLinkedList<ColumnBase> columnsByVisiblePosition = sourceContext.ColumnsByVisiblePosition; SelectedItemsStorage selectedColumnStore = new SelectedItemsStorage( null, 8 ); SelectionRange fullColumnRange = new SelectionRange( 0, columnsByVisiblePosition.Count - 1 ); selectedColumnStore.Add( new SelectionRangeWithItems( fullColumnRange, null ) ); int index = 0; foreach( ColumnBase column in columnsByVisiblePosition ) { if( !column.Visible ) { selectedColumnStore.Remove( new SelectionRangeWithItems( new SelectionRange( index ), null ) ); } index++; } int columnRangeCount = selectedColumnStore.Count; for( int i = 0; i < columnRangeCount; i++ ) { sourceContext.DataGridControl.SelectionChangerManager.SelectCells( sourceContext, new SelectionCellRangeWithItems( itemRange, items, selectedColumnStore[ i ].Range ) ); } } }
public bool SelectItems(SelectionRangeWithItems rangeWithItems) { SelectionRange range = rangeWithItems.Range; if (!(m_owner.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase)) { if (range.IsEmpty) { foreach (object item in rangeWithItems.Items) { if (!m_toDeferSelect.Contains(item)) { m_toDeferSelect.Add(item); } } return(false); } } else { if (range.IsEmpty) { throw new ArgumentException("rangeWithItems.Range can't be empty when we are using a DataGridVirtualizingCollectionView", "rangeWithItems"); } } if (rangeWithItems.Length == 1) { if (!m_itemsToUnselect.Remove(rangeWithItems)) { if (m_owner.SelectedItemsStore.Contains(rangeWithItems)) { return(false); } if (m_itemsToSelect.Contains(range)) { return(false); } this.m_itemsToSelect.Add(rangeWithItems); } return(true); } else { bool selectionChanged = m_itemsToUnselect.Remove(rangeWithItems); SelectedItemsStorage tempStorage = new SelectedItemsStorage(m_owner); tempStorage.Add(rangeWithItems); // Remove the currently selected item from the new range to select foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_owner.SelectedItemsStore) { tempStorage.Remove(existingSelectionRangeWithItems); } // Remove the pending item to be selected from the new range to select foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect) { tempStorage.Remove(existingSelectionRangeWithItems); } if (tempStorage.Count > 0) { selectionChanged = true; foreach (SelectionRangeWithItems rangeWithItemsToAdd in tempStorage) { m_itemsToSelect.Add(rangeWithItemsToAdd); } } return(selectionChanged); } }
public bool UnselectItems(SelectionRangeWithItems rangeWithItems) { if (!(m_owner.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase)) { if (m_toDeferSelect.Count > 0) { foreach (object item in rangeWithItems.Items) { m_toDeferSelect.Remove(item); } } } SelectionRange range = rangeWithItems.Range; if (range.IsEmpty) { // We have no index we have to remove based on item bool selectionChanged = false; List <SelectionRangeWithItems> itemsRangeToRemove = new List <SelectionRangeWithItems>(); List <object> itemsToUnselect = new List <object>(rangeWithItems.Items); int count = itemsToUnselect.Count; for (int i = count - 1; i >= 0; i--) { object itemToUnselect = itemsToUnselect[i]; bool selectionAdded = false; foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect) { int index = Array.IndexOf(existingSelectionRangeWithItems.Items, itemToUnselect); if (index > -1) { selectionAdded = true; itemsRangeToRemove.Add( new SelectionRangeWithItems( existingSelectionRangeWithItems.Range.GetIndexFromItemOffset(index), itemToUnselect)); } } if (selectionAdded) { itemsToUnselect.RemoveAt(i); } } // Remove the currently unselected item from the new range to select foreach (SelectionRangeWithItems itemRangeToRemove in itemsRangeToRemove) { selectionChanged |= m_itemsToSelect.Remove(itemRangeToRemove); } count = itemsToUnselect.Count; for (int i = 0; i < count; i++) { object itemToUnselect = itemsToUnselect[i]; foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_owner.SelectedItemsStore) { int index = Array.IndexOf(existingSelectionRangeWithItems.Items, itemToUnselect); if (index >= 0) { index = existingSelectionRangeWithItems.Range.GetIndexFromItemOffset(index); if (!m_itemsToUnselect.Contains(index)) { selectionChanged = true; m_itemsToUnselect.Add(new SelectionRangeWithItems(index, itemToUnselect)); } } } } return(selectionChanged); } if (range.Length == 1) { if (!m_itemsToSelect.Remove(rangeWithItems)) { if (!m_owner.SelectedItemsStore.Contains(range)) { return(false); } if (m_itemsToUnselect.Contains(range)) { return(false); } m_itemsToUnselect.Add(rangeWithItems); } return(true); } else { SelectedItemsStorage tempStorage = new SelectedItemsStorage(m_owner); tempStorage.Add(rangeWithItems); // Remove the currently selected item from the new range to select foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect) { if (!range.Intersect(existingSelectionRangeWithItems.Range).IsEmpty) { tempStorage.Remove(existingSelectionRangeWithItems); } } bool selectionChanged = m_itemsToSelect.Remove(rangeWithItems); if (tempStorage.Count > 0) { selectionChanged = true; foreach (SelectionRangeWithItems rangeWithItemsToAdd in tempStorage) { Debug.Assert(!m_itemsToUnselect.Contains(rangeWithItemsToAdd.Range)); m_itemsToUnselect.Add(rangeWithItemsToAdd); } } return(selectionChanged); } }
public void Visit(DataGridContext sourceContext, ref bool stopVisit) { object[] items; CollectionView itemsCollection = sourceContext.Items; int count = itemsCollection.Count; if (count == 0) { return; } if (sourceContext.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase) { items = null; } else { items = new object[count]; for (int i = 0; i < count; i++) { items[i] = itemsCollection.GetItemAt(i); } } SelectionRange itemRange = new SelectionRange(0, count - 1); if (sourceContext.DataGridControl.SelectionUnit == SelectionUnit.Row) { sourceContext.DataGridControl.SelectionChangerManager.SelectItems( sourceContext, new SelectionRangeWithItems(itemRange, items)); } else { HashedLinkedList <ColumnBase> columnsByVisiblePosition = sourceContext.ColumnsByVisiblePosition; SelectedItemsStorage selectedColumnStore = new SelectedItemsStorage(null); SelectionRange fullColumnRange = new SelectionRange(0, columnsByVisiblePosition.Count - 1); selectedColumnStore.Add(new SelectionRangeWithItems(fullColumnRange, null)); int index = 0; foreach (ColumnBase column in columnsByVisiblePosition) { if (!column.Visible) { selectedColumnStore.Remove(new SelectionRangeWithItems(new SelectionRange(index), null)); } index++; } int columnRangeCount = selectedColumnStore.Count; for (int i = 0; i < columnRangeCount; i++) { sourceContext.DataGridControl.SelectionChangerManager.SelectCells( sourceContext, new SelectionCellRangeWithItems(itemRange, items, selectedColumnStore[i].Range)); } } }
private void DoRangeSelection( DataGridContext oldCurrentDataGridContext, object oldCurrentItem, int oldCurrentColumnIndex, DataGridContext dataGridContext, int sourceDataItemIndex, object item, int columnIndex, bool keepPreviousSelection, bool fromRowSelector ) { if( item == null ) return; IDataGridContextVisitable visitable = ( IDataGridContextVisitable )m_owner.DataGridContext; if( m_rangeSelectionItemStartAnchor == -1 ) { if( ( oldCurrentDataGridContext != null ) && ( oldCurrentItem != null ) ) { m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( oldCurrentDataGridContext, oldCurrentItem ); m_rangeSelectionColumnStartAnchor = oldCurrentColumnIndex; } else { m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item ); m_rangeSelectionColumnStartAnchor = columnIndex; } } if( !keepPreviousSelection ) { this.UnselectAll(); } int starting_index = m_rangeSelectionItemStartAnchor; int ending_index = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item ); int min = Math.Min( starting_index, ending_index ); int max = Math.Max( starting_index, ending_index ); // here I need to normalize the values to ensure that I'm not catching the Fixed Headers or Fixed Footers if( ( min != -1 ) && ( max != -1 ) ) { SelectionRange[] selectedColumns; if( m_owner.SelectionUnit == SelectionUnit.Row ) { selectedColumns = null; } else { SelectionRange fullColumnRange; // If called from a RowSelector, we do a range // selection using all the columns no matter // what was the previous anchor if( fromRowSelector ) { m_rangeSelectionColumnStartAnchor = 0; fullColumnRange = new SelectionRange( 0, Math.Max( 0, dataGridContext.ColumnsByVisiblePosition.Count - 1 ) ); } else { if( m_rangeSelectionColumnStartAnchor == -1 ) m_rangeSelectionColumnStartAnchor = 0; if( columnIndex == -1 ) return; fullColumnRange = new SelectionRange( m_rangeSelectionColumnStartAnchor, columnIndex ); } SelectedItemsStorage selectedColumnStore = new SelectedItemsStorage( null, 8 ); selectedColumnStore.Add( new SelectionRangeWithItems( fullColumnRange, null ) ); int index = 0; foreach( ColumnBase column in dataGridContext.ColumnsByVisiblePosition ) { if( !column.Visible ) { selectedColumnStore.Remove( new SelectionRangeWithItems( new SelectionRange( index ), null ) ); } index++; } selectedColumns = selectedColumnStore.ToSelectionRangeArray(); } bool visitWasStopped; visitable.AcceptVisitor( min, max, new RangeSelectionVisitor( selectedColumns ), DataGridContextVisitorType.ItemsBlock, out visitWasStopped ); } }
public bool SelectItems( SelectionRangeWithItems rangeWithItems ) { SelectionRange range = rangeWithItems.Range; if( !( m_owner.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase ) ) { if( range.IsEmpty ) { foreach( object item in rangeWithItems.Items ) { if( !m_toDeferSelect.Contains( item ) ) m_toDeferSelect.Add( item ); } return false; } } else { if( range.IsEmpty ) throw new ArgumentException( "rangeWithItems.Range can't be empty when we are using a DataGridVirtualizingCollectionView", "rangeWithItems" ); } if( rangeWithItems.Length == 1 ) { if( !m_itemsToUnselect.Remove( rangeWithItems ) ) { if( m_owner.SelectedItemsStore.Contains( rangeWithItems ) ) return false; if( m_itemsToSelect.Contains( range ) ) return false; this.m_itemsToSelect.Add( rangeWithItems ); } return true; } else { bool selectionChanged = m_itemsToUnselect.Remove( rangeWithItems ); SelectedItemsStorage tempStorage = new SelectedItemsStorage( m_owner, 8 ); tempStorage.Add( rangeWithItems ); // Remove the currently selected item from the new range to select foreach( SelectionRangeWithItems existingSelectionRangeWithItems in m_owner.SelectedItemsStore ) { tempStorage.Remove( existingSelectionRangeWithItems ); } // Remove the pending item to be selected from the new range to select foreach( SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect ) { tempStorage.Remove( existingSelectionRangeWithItems ); } if( tempStorage.Count > 0 ) { selectionChanged = true; foreach( SelectionRangeWithItems rangeWithItemsToAdd in tempStorage ) { m_itemsToSelect.Add( rangeWithItemsToAdd ); } } return selectionChanged; } }
public bool UnselectItems( SelectionRangeWithItems rangeWithItems ) { if( !( m_owner.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase ) ) { if( m_toDeferSelect.Count > 0 ) { foreach( object item in rangeWithItems.Items ) { m_toDeferSelect.Remove( item ); } } } SelectionRange range = rangeWithItems.Range; if( range.IsEmpty ) { // We have no index we have to remove based on item bool selectionChanged = false; List<SelectionRangeWithItems> itemsRangeToRemove = new List<SelectionRangeWithItems>(); List<object> itemsToUnselect = new List<object>( rangeWithItems.Items ); int count = itemsToUnselect.Count; for( int i = count - 1; i >= 0; i-- ) { object itemToUnselect = itemsToUnselect[ i ]; bool selectionAdded = false; foreach( SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect ) { int index = Array.IndexOf( existingSelectionRangeWithItems.Items, itemToUnselect ); if( index > -1 ) { selectionAdded = true; itemsRangeToRemove.Add( new SelectionRangeWithItems( existingSelectionRangeWithItems.Range.GetIndexFromItemOffset( index ), itemToUnselect ) ); } } if( selectionAdded ) { itemsToUnselect.RemoveAt( i ); } } // Remove the currently unselected item from the new range to select foreach( SelectionRangeWithItems itemRangeToRemove in itemsRangeToRemove ) { selectionChanged |= m_itemsToSelect.Remove( itemRangeToRemove ); } count = itemsToUnselect.Count; for( int i = 0; i < count; i++ ) { object itemToUnselect = itemsToUnselect[ i ]; foreach( SelectionRangeWithItems existingSelectionRangeWithItems in m_owner.SelectedItemsStore ) { int index = Array.IndexOf( existingSelectionRangeWithItems.Items, itemToUnselect ); if( index > -1 ) { index = existingSelectionRangeWithItems.Range.GetIndexFromItemOffset( index ); if( !m_itemsToUnselect.Contains( index ) ) { selectionChanged = true; m_itemsToUnselect.Add( new SelectionRangeWithItems( index, itemToUnselect ) ); } } } } return selectionChanged; } if( range.Length == 1 ) { if( !m_itemsToSelect.Remove( rangeWithItems ) ) { if( !m_owner.SelectedItemsStore.Contains( range ) ) return false; if( m_itemsToUnselect.Contains( range ) ) return false; m_itemsToUnselect.Add( rangeWithItems ); } return true; } else { SelectedItemsStorage tempStorage = new SelectedItemsStorage( m_owner, 8 ); tempStorage.Add( rangeWithItems ); // Remove the currently selected item from the new range to select foreach( SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect ) { if( !range.Intersect( existingSelectionRangeWithItems.Range ).IsEmpty ) { tempStorage.Remove( existingSelectionRangeWithItems ); } } bool selectionChanged = m_itemsToSelect.Remove( rangeWithItems ); if( tempStorage.Count > 0 ) { selectionChanged = true; foreach( SelectionRangeWithItems rangeWithItemsToAdd in tempStorage ) { Debug.Assert( !m_itemsToUnselect.Contains( rangeWithItemsToAdd.Range ) ); m_itemsToUnselect.Add( rangeWithItemsToAdd ); } } return selectionChanged; } }
public SelectionInfo GetSelectionInfo() { List<SelectionRangeWithItems> removedRangeWithItems; List<SelectionRangeWithItems> unselectedItemsFromRemove = this.GetUnselectedItemsFromRemove( out removedRangeWithItems ); List<SelectionCellRangeWithItems> removedCellsRangeWithItems; List<SelectionCellRangeWithItems> unselectedCellsFromRemove = this.GetUnselectedCellsFromRemove( out removedCellsRangeWithItems ); SelectedItemsStorage itemsToUnselect = new SelectedItemsStorage( m_itemsToUnselect ); SelectedCellsStorage cellsToUnselect = new SelectedCellsStorage( m_cellsToUnselect ); foreach( SelectionRangeWithItems rangeWithItems in unselectedItemsFromRemove ) { itemsToUnselect.Add( rangeWithItems ); } foreach( SelectionCellRangeWithItems cellRangeWithItems in unselectedCellsFromRemove ) { cellsToUnselect.Add( cellRangeWithItems ); } return new SelectionInfo( m_owner, itemsToUnselect, new SelectedItemsStorage( m_itemsToSelect ), cellsToUnselect, new SelectedCellsStorage( m_cellsToSelect ) ); }